创建线性布局时出现NullPointerException

时间:2012-11-07 20:53:25

标签: android android-layout nullpointerexception android-linearlayout

我的代码给了我一个NullPointerException并且我已经阅读了posts suggesting a clean-and-rebuild但是到目前为止这种方法对我没有帮助。

我的堆栈跟踪如下:

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中包含一个按钮。这有点搞砸了。从那时起我就一直遇到问题。

非常感谢任何帮助!提前谢谢!

3 个答案:

答案 0 :(得分:3)

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

setContentView(R.layout.my_xml_file);

答案 1 :(得分:2)

看起来您正在尝试合并两个不兼容的步骤。第一步是告诉系统要扩展和显示哪个XML文件,以便你想做别人的建议:

setContentView(R.layout.xml_layout_filename);

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

的内容
Button button = (Button)findViewById(R.id.button1);
DrawView drawView = (DrawView)findViewById(R.id.drawView1);

答案 2 :(得分:1)

这应该是:

setContentView(R.layout.your_xml_file_name_here);