android TextView设置文本问题

时间:2014-01-28 00:04:09

标签: java android eclipse android-intent textview

我是android的新手,并试图显示一条从意图传递为额外字符串的消息,代码如下:

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);
    }

}

我得到的错误如下:

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

我还在display_message中将strings.xml定义为<string name="display_message">Display a message</string>,并在活动中启用了操作栏的overlay模式,并设置了android:paddingTop="?attr/actionBarSize"活动布局文件。所以我想知道如何解决这些问题?

欢呼声

2 个答案:

答案 0 :(得分:5)

在检索UI元素之前移动setContentView

即,您需要先扩充布局,否则findViewById会返回null,因此行NullPointerException会返回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);

答案 1 :(得分:2)

第一:

setContentView(R.layout.activity_display_message);  

此方法用于通过活动引用您的布局,因此您需要在将活动与布局链接后获取布局UI引用,因此您需要在setContentView方法

之后移动任何UI引用

第二:

此代码

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

首先需要检查intent是否有额外的数据,如果它没有数据代码会给你空指针异常,因为你想通过添加这个代码来访问getStringExtra的null对象将保护你免受这个错误

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