最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Android中调用startActivity结果导致:java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException

Android crifan 881浏览 0评论

【问题】

android中,为了调试,加了直接跳转到另外一个UI的代码:

	void testTab(){
		startActivity(new Intent (this, MainActivity.class) );
	}

结果导致挂掉:

03-27 13:57:03.115: E/AndroidRuntime(12680): FATAL EXCEPTION: main
03-27 13:57:03.115: E/AndroidRuntime(12680): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.MainActivity}: java.lang.NullPointerException
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.os.Looper.loop(Looper.java:176)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread.main(ActivityThread.java:5279)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at dalvik.system.NativeStart.main(Native Method)
03-27 13:57:03.115: E/AndroidRuntime(12680): Caused by: java.lang.NullPointerException
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at xxx.MainActivity.onCreate(MainActivity.java:152)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.Activity.performCreate(Activity.java:5267)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-27 13:57:03.115: E/AndroidRuntime(12680): 	... 11 more

如图:

android java.lang.RuntimeException Unable to start activity ComponentInfo

【解决过程】

1.去掉了上述代码,的确就不会挂了。

但是奇怪的是,之前加了上述代码,是不会挂的啊。

2.难道是因为MainActivity.java中,改为了:

public class MainActivity extends FragmentActivity implements ISubscriber{

而导致的?

3.参考:

android – androidruntime java.lang.runtimeexception unable to start activity componentinfo java.lang.classcast exception – Stack Overflow

android – Unable to start activity ComponentInfo{…}: java.lang.NullPointerException – Stack Overflow

android – java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException – Stack Overflow

没用。

4.参考:

nullpointerexception – java.lang.RuntimeException: Unable to start activity in android app – Stack Overflow

想到了,双击之前出错的位置,从而自动跳转到出错的代码的位置:

click AndroidRuntime at MainActivity.onCreate MainActivity.java 152 line jump to code

接着自己去调试而找到根本原因:

some value is null when onCreate

此处,在MainActivity.java中的onCreate中,之前正常的逻辑是从另外一个Activity中跳过来到MainActivity

所以那时候正常传递过来的

bundle=this.getIntent().getExtras();

中可以获得所需要的

String addString=bundle.getString("address");

而此处,为了调试,直接跳转过去,没有传递参数,而导致bundle为null,从而出错了。

【总结】

解决办法,要么是startActivity跳转到MainActivity之前,传递对应的参数-》确保传递;

要么暂时把MainActivity中那段代码注释掉->只是为了确保继续可以调试就行了。

转载请注明:在路上 » 【已解决】Android中调用startActivity结果导致:java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
13 queries in 0.221 seconds, using 9.98MB memory