我正在开发一个应用,其中用户将一个数字输入到一个活动中,并且该数字显示在下一个活动中。这是我到目前为止所做的。
第一项活动:EnterText.java
public class EnterText extends Activity {
//This is the string that will be passed to the next activity
public static String mynumber = "com.example.ListviewExplorer.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_text, menu);
return true;
}
//This is where onClick, the button will invoke sendMessage
//sendMessage is supposed to start the next activity
public void sendMessage(View view){
Intent intent = new Intent (this, DisplayText.class);
EditText editText = (EditText) findViewById(R.id.editText1);
mynumber = editText.getText().toString();
intent.putExtra("MyNumber",mynumber);
startActivity(intent);
}
}
第二项活动:DisplayText.java
public class DisplayText extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
String mynumber = extras.getString("MyNumber");
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
// Show the Up button in the action bar.
setupActionBar();
}
}
问题是,当我按下第一个活动上的按钮并调用sendMessage时,此应用程序崩溃;第二项活动甚至没有显示出来。
有谁知道为什么会这样?
LogCat
发布在下方。
开始吧!
05-29 11:57:59.385: W/dalvikvm(3046): threadid=1: thread exiting with uncaught exception (group=0x42d5d140)
05-29 11:57:59.425: E/AndroidRuntime(3046): FATAL EXCEPTION: main
05-29 11:57:59.425: E/AndroidRuntime(3046): Process: com.example.listviewexplorer, PID: 3046
05-29 11:57:59.425: E/AndroidRuntime(3046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewexplorer/com.example.listviewexplorer.DisplayText}: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Looper.loop(Looper.java:136)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-29 11:57:59.425: E/AndroidRuntime(3046): at dalvik.system.NativeStart.main(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): Caused by: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.example.listviewexplorer.DisplayText.onCreate(DisplayText.java:21)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Activity.performCreate(Activity.java:5231)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-29 11:57:59.425: E/AndroidRuntime(3046): ... 11 more
答案 0 :(得分:3)
您需要在使用之前指定布局。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// assign layout
setContentView(R.layout.activity_display_text);
// then you can use it
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
...
}
由于尚未定义布局,因此它位于该阶段null
,当您尝试访问其中的视图时,您将获得NullPointerException
。
答案 1 :(得分:2)
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
在setContentView()
之前findViewById()
将返回null,无论如何,并且在null引用上调用方法会导致在堆栈跟踪中看到NPE。
将setContentView()
移到findViewById()
上方,假设textView1
实际上位于activity_display_text
布局中。
这解决了NPE崩溃的问题。要实际传递一个值,请查看其他一些答案。