出于某种原因,每次尝试启动我的应用程序时,我都会出现以下错误:
Unable to instantiate activity ComponentInfo{com.example.lifeahead/com.example.lifeahead.MainActivity}:java.lang.NullPointerException
我检查了清单文件,并添加了所有活动.
我启动应用程序时使用的唯一方法是:
private voID logIn(){ button logIn = (button) findVIEwByID(R.ID.login); logIn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub Intent loginIntent = new Intent(MainActivity.this, HomeActivity.class); loginIntent.putExtra("cmUN", cmUN); loginIntent.putExtra("cmPW", cmPW); startActivity(loginIntent); } });}
这是onCreate:
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.ID.container, new PlaceholderFragment()).commit(); } logIn();}
完整日志:
Ë
/AndroIDRuntime(1607): FATAL EXCEPTION: mainE/AndroIDRuntime(1607): Process: com.example.lifeahead, PID: 1607E/AndroIDRuntime(1607): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lifeahead/com.example.lifeahead.MainActivity}: java.lang.NullPointerExceptionE/AndroIDRuntime(1607): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2121)E/AndroIDRuntime(1607): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)E/AndroIDRuntime(1607): at androID.app.ActivityThread.access0(ActivityThread.java:135)E/AndroIDRuntime(1607): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)E/AndroIDRuntime(1607): at androID.os.Handler.dispatchMessage(Handler.java:102)E/AndroIDRuntime(1607): at androID.os.Looper.loop(Looper.java:136)E/AndroIDRuntime(1607): at androID.app.ActivityThread.main(ActivityThread.java:5017)E/AndroIDRuntime(1607): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroIDRuntime(1607): at java.lang.reflect.Method.invoke(Method.java:515)E/AndroIDRuntime(1607): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)E/AndroIDRuntime(1607): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:595)E/AndroIDRuntime(1607): at dalvik.system.NativeStart.main(Native Method)E/AndroIDRuntime(1607): Caused by: java.lang.NullPointerExceptionE/AndroIDRuntime(1607): at androID.app.Activity.findVIEwByID(Activity.java:1884)E/AndroIDRuntime(1607): at com.example.lifeahead.MainActivity.<init>(MainActivity.java:22)E/AndroIDRuntime(1607): at java.lang.class.newInstanceImpl(Native Method)E/AndroIDRuntime(1607): at java.lang.class.newInstance(Class.java:1208)E/AndroIDRuntime(1607): at androID.app.Instrumentation.newActivity(Instrumentation.java:1061)E/AndroIDRuntime(1607): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2112)E/AndroIDRuntime(1607): ... 11 more
我感谢所有帮助!
解决方法:
stacktrace告诉你
button logIn = (button) findVIEwByID(R.ID.login);
findVIEwByID返回的对象为null,因为您拥有的唯一方法是logIn(). findVIEwByID返回null的唯一原因是因为您正在查找不属于当前活动的视图层次结构的视图
总结以上是内存溢出为你收集整理的Android java.lang.RuntimeException:无法实例化活动ComponentInfo … java.lang.NullPointerException全部内容,希望文章能够帮你解决Android java.lang.RuntimeException:无法实例化活动ComponentInfo … java.lang.NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)