1

我的代码给了我一个NullPointerException异常,并且我已经通过posts suggesting a clean-and-rebuild进行了读取,但该方法至今尚未帮助我。创建线性布局时出现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的按钮。这是它有点搞砸了。自那以后我一直有问题。

任何帮助将非常感激!提前致谢!

回答

3

它不会工作,因为你所呼叫的setContentView()一个前findViewById()方法,所以它会搜索它还未创建的视图。用这个代替:

setContentView(R.layout.my_xml_file); 
+0

感谢您的快速响应!有用! – Antimony

+0

由于您解释了错误,因此我正在标记您的最佳答案。谢谢。代码现在更有意义了! :P – Antimony

1

这应该是:

setContentView(R.layout.your_xml_file_name_here); 
+0

感谢您的快速回复!它的工作原理:D – Antimony

2

看起来你试图将两个不兼容的步骤结合起来。第一步是告诉系统膨胀和显示该XML文件,所以你想要做其他人所说的:

setContentView(R.layout.xml_layout_filename); 

然后,第二步获得按钮或DrawView或其他控制,使用的东西像

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