空指针异常错误
Logcat
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.os.Looper.loop(Looper.java:123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at java.lang.reflect.Method.invoke(Method.java:521)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at dalvik.system.NativeStart.main(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): Caused by: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at one.two.Booking.onCreate(Booking.java:50)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
Booking.java
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
this.setContentView(R.layout.booking);
txtArrivalTime = (TextView) findViewById(R.id.txtArrivalTime); // Member assignment
txtFerry = (TextView) findViewById(R.id.txtFerry);
txtStatus = (TextView) findViewById(R.id.txtStatus);
txtDestination = (TextView) findViewById(R.id.txtDestination);
colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
Button btnBook = (Button) findViewById(R.id.btnBook);
db.open();
fillData();
db.close();
colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
b = this.getIntent().getExtras();
s = b.getString("DEFAULTTEXT");
btnBook.setOnClickListener(new MyOnClickListener());
}
第 50 行是 s = b.getString("DEFAULTTEXT");
谢谢。
Logcat
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.os.Looper.loop(Looper.java:123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at java.lang.reflect.Method.invoke(Method.java:521)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at dalvik.system.NativeStart.main(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): Caused by: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at one.two.Booking.onCreate(Booking.java:50)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
Booking.java
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
this.setContentView(R.layout.booking);
txtArrivalTime = (TextView) findViewById(R.id.txtArrivalTime); // Member assignment
txtFerry = (TextView) findViewById(R.id.txtFerry);
txtStatus = (TextView) findViewById(R.id.txtStatus);
txtDestination = (TextView) findViewById(R.id.txtDestination);
colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
Button btnBook = (Button) findViewById(R.id.btnBook);
db.open();
fillData();
db.close();
colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
b = this.getIntent().getExtras();
s = b.getString("DEFAULTTEXT");
btnBook.setOnClickListener(new MyOnClickListener());
}
Line 50 is s = b.getString("DEFAULTTEXT");
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是这样:
使用 getStringExtra(String)
从 getExtras() 获得的包为 null,因此出现异常
Instead of this:
Use getStringExtra(String)
The bundle you get from getExtras() is null, hence the exception