1

我的代码给了我一个 NullPointerException 并且我已经阅读了建议进行清理和重建的帖子,但到目前为止这种方法对我没有帮助。

我的堆栈跟踪如下:

E/AndroidRuntime(553): FATAL EXCEPTION: main
E/AndroidRuntime(553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.touchlogger/com.example.touchlogger.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(553):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(553):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(553):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(553):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(553): Caused by: java.lang.NullPointerException
E/AndroidRuntime(553):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3336)
E/AndroidRuntime(553):  at android.view.ViewGroup.addView(ViewGroup.java:3208)
E/AndroidRuntime(553):  at android.view.ViewGroup.addView(ViewGroup.java:3188)
E/AndroidRuntime(553):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
E/AndroidRuntime(553):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
E/AndroidRuntime(553):  at android.app.Activity.setContentView(Activity.java:1855)
E/AndroidRuntime(553):  at com.example.touchlogger.MainActivity.onCreate(MainActivity.java:30)
E/AndroidRuntime(553):  at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(553):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(553):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

我的 XML 文件如下:

<com.example.touchlogger.DrawView
    android:id="@+id/drawView1"
    android:layout_width="wrap_content"
    android:layout_height="357dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="160dp"
    android:layout_height="49dp"
    android:layout_gravity="center|center_vertical"
    android:layout_marginBottom="29dp"
    android:text="End" />

</LinearLayout>

导致错误的行是:

setContentView((LinearLayout)(findViewById(R.id.layout)));

我是 Android 编程新手,即使问题很简单,恐怕它对我来说根本没有任何意义。我在这上面花了两天多的时间,我完全迷失了。我的代码一直在运行,直到我决定在 drawView 中包含一个按钮。这就是它有点搞砸的地方。从那以后我就一直遇到问题。

任何帮助将不胜感激!提前致谢!

4

3 回答 3

3

它不起作用,因为您在调用该findViewById()方法之前调用了该方法setContentView(),因此它将搜索尚未创建的视图。改用这个:

setContentView(R.layout.my_xml_file);
于 2012-11-07T20:58:16.620 回答
2

看起来您正在尝试组合两个不兼容的步骤。第一步是告诉系统要扩充和显示哪个 XML 文件,因此您要按照其他人的建议进行操作:

setContentView(R.layout.xml_layout_filename);

然后,获取按钮或其他控件的第二步DrawView,使用类似

Button button = (Button)findViewById(R.id.button1);
DrawView drawView = (DrawView)findViewById(R.id.drawView1);
于 2012-11-07T21:02:26.253 回答
1

这应该是:

setContentView(R.layout.your_xml_file_name_here);
于 2012-11-07T20:55:42.090 回答