我的代碼給了我一個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的按鈕。這是它有點搞砸了。自那以後我一直有問題。
任何幫助將非常感激!提前致謝!
感謝您的快速響應!有用! – Antimony
由於您解釋了錯誤,因此我正在標記您的最佳答案。謝謝。代碼現在更有意義了! :P – Antimony