1
我正在嘗試製作其屏幕始終處於橫向模式的應用程序。在button.setonclick監聽器上拋出空指針異常
我用setRequestedOrientation()函數來實現。我用一些按鈕來創建一個計算器。
但是,當我試圖檢查它的一些功能時,通過在模擬器上運行它,我得到了錯誤Unfortunately, Your app has been stopped
。
基本上,它是按nullpointer exception
在按鈕的setOnClickListener
功能。我不知道爲什麼如此。 logcat錯誤:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john.mycalci/com.example.john.mycalci.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.john.mycalci.MainActivity.onCreate(MainActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
這是我的java代碼。我想使用layout-land
資源。
的可能的複製[什麼是空指針異常,怎麼解決呢?(http://stackoverflow.com/questions/218384/what-is-a -nullpointerexception-and-how-do-i-fix-it) – Turing85
確保所有的ID都可用於這兩種佈局 – FlanschiFox
哪個按鈕使應用程序崩潰? –