daiyue
daiyue

Reputation: 7458

android TextView set text issue

I am new to android and tried to display a message passed as a extra string from a intent, the code is like follows:

public class DisplayMessageActivity extends /*Activity*/ActionBarActivity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = (TextView) findViewById(R.id.display_message);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(R.layout.activity_display_message);

    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }else{
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.display_message, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
        }
    return super.onOptionsItemSelected(item);
    }

}

and I got the errors as below:

01-27 23:45:05.864: E/AndroidRuntime(528): FATAL EXCEPTION: main
01-27 23:45:05.864: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:137)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-27 23:45:05.864: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
01-27 23:45:05.864: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:511)
01-27 23:45:05.864: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 23:45:05.864: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 23:45:05.864: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)
01-27 23:45:05.864: E/AndroidRuntime(528): Caused by: java.lang.NullPointerException
01-27 23:45:05.864: E/AndroidRuntime(528):  at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:29)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.Activity.performCreate(Activity.java:4465)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-27 23:45:05.864: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-27 23:45:05.864: E/AndroidRuntime(528):  ... 11 more

I have also defined display_message in strings.xml as <string name="display_message">Display a message</string>, and enabled the overlay mode of the action bar in the activity, and set android:paddingTop="?attr/actionBarSize" for the activity layout file. So I wonder how to solve the issues?

cheers

Upvotes: 0

Views: 1002

Answers (2)

Alexis C.
Alexis C.

Reputation: 93872

Move setContentView before retrieving your UI elements.

I.e, you need to inflate your layout before, otherwise findViewById returns null and hence the NullPointerException at the line textView.setTextSize(40);.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    /****/
    TextView textView = (TextView) findViewById(R.id.display_message);
    textView.setTextSize(40);
    textView.setText(message);

Upvotes: 5

mohammed momn
mohammed momn

Reputation: 3210

First :

setContentView(R.layout.activity_display_message);  

this method is used to reference your layout with activity so you need to get the layout UI references after you link the activity with layout so you need to move any UI references after setContentView method

Second :

this code

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

you need first to check if intent has extra data cause if it doesn't has data code will give you null pointer exception because you want to access getStringExtra of null object by adding this code will protect you from this error

Intent intent = getIntent();            
        if(intent.getExtras() !=null){
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        }

Upvotes: 2

Related Questions