出于某种原因,我的应用程序在我正在测试的平板电脑上崩溃,但手机上没有。我不确定是什么原因造成的。它发生在应用程序的不同部分,但总是在从一个屏幕移动到另一个屏幕时。
以下是似乎导致应用崩溃的代码:
public void About(View view)
{
Intent i = new Intent();
i.setClassName("com.example","com.example.timestableseasy.About");
startActivity(i);
}
about.java
public class About extends Activity
{
TextView home;
@Override
public void onBackPressed() {
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
home = (TextView) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("Exercise", "button clicked");
Intent i = new Intent();
i.setClassName("com.example","com.example.timestableseasy.Menu2");
startActivity(i);
}
});
}
}
这里是logcat:
06-16 16:20:59.465: D/AndroidRuntime(6281): Shutting down VM
06-16 16:20:59.465: W/dalvikvm(6281): threadid=1: thread exiting with uncaught exception (group=0x412f2930)
06-16 16:20:59.473: E/AndroidRuntime(6281): FATAL EXCEPTION: main
06-16 16:20:59.473: E/AndroidRuntime(6281): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.timestableseasy.About}: java.lang.NullPointerException
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.os.Looper.loop(Looper.java:175)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread.main(ActivityThread.java:5279)
06-16 16:20:59.473: E/AndroidRuntime(6281): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 16:20:59.473: E/AndroidRuntime(6281): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 16:20:59.473: E/AndroidRuntime(6281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-16 16:20:59.473: E/AndroidRuntime(6281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-16 16:20:59.473: E/AndroidRuntime(6281): at dalvik.system.NativeStart.main(Native Method)
06-16 16:20:59.473: E/AndroidRuntime(6281): Caused by: java.lang.NullPointerException
06-16 16:20:59.473: E/AndroidRuntime(6281): at com.example.timestableseasy.About.onCreate(About.java:25)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.Activity.performCreate(Activity.java:5283)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-16 16:20:59.473: E/AndroidRuntime(6281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-16 16:20:59.473: E/AndroidRuntime(6281): ... 11 more
之前有效,但出于某种原因在上周停止了工作。
编辑:我已经消除了大型布局文件中出现错误的可能性 - 强制应用程序通过删除大文件来加载正常版本,按下按钮时仍然会显示相同的错误屏幕。答案 0 :(得分:0)
NPE可能发生的唯一地方就在这条线上:
home.setOnClickListener(new View.OnClickListener() ...
这意味着这一行:
home = (TextView) findViewById(R.id.home);
返回null
。确保id="@+id/home"
文件中的TextView
之一确实有activity_about.xml
。
请注意,手机和平板电脑可能有单独的布局文件。通常,对于手机,布局来自res / layout文件夹,对于平板电脑 - 来自res / layout-sw600dp - 只有当缺少那个时才会使用res / layout中的布局。确实没有其他地方可以发生此错误。