我搜索了其他类似的问题,我找到了一些,但奇怪的是它们对我不起作用,我不知道我做错了什么......
public class MainActivity extends Activity {
EditText item;
Button insert;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item = (EditText) findViewById(R.id.item);
layout = (RelativeLayout) findViewById(R.id.layout);
}
@Override
public void onResume() {
super.onResume();
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s_item = item.getText().toString();
TextView tv = new TextView(MainActivity.this);
layout.addView(tv);
tv.setText(s_item);
}
});
}
}
当应用程序启动时,它只是崩溃了......问题显然出在这段代码中......
LOGCAT
08-18 14:37:19.425 2981-2981/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {g.d.raven/g.d.raven.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at g.d.raven.MainActivity.onResume(MainActivity.java:30)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3832)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
... 12 more
08-18 14:37:19.429 130-143/? E/: Dumpstate > /data/log/dumpstate_app_error
谢谢大家,刚刚自己找到了,忘记实例化按钮了XD