-2
嘿傢伙,所以當我打這個特定的按鈕,它應該帶我到另一個佈局。然而,每次我點擊它,整個應用程序崩潰。下面是該網頁即時試圖去的onCreate方法:當我按下按鈕時,Android應用程序崩潰
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bench_data_entry);
//opens the database
openDB();
//Initializes and declares the sensors, and accelerometer
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
// tv.setText("Aasfdaf");
}
這裏是的onClick的按鈕,我按:
public void onClick_Bench(View v){
//Intent to create a new activity
Intent intent = new Intent(MainMenuActivity.this, BenchDataEntry.class);
//Goes to the Data Entry Screen
startActivity(intent);
}
,這裏是錯誤日誌中我收到的時候我按下按鈕:
08-08 17:30:30.785 31113-31113/com.example.jacob.repcapture E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jacob.repcapture, PID: 31113
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jacob.repcapture/com.example.jacob.repcapture.BenchDataEntry}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2096)
at com.example.jacob.repcapture.BenchDataEntry.<init>(BenchDataEntry.java:49)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
'findViewById'必須在'onCreate'方法內。不在其外面。請參閱BenchDataEntry.java的第49行 –
實際上,只需通過執行_act.runOnUiThread _act爲Activity變量或此操作,就可以在UI線程上調用findViewById。 另外,如果您最終在兩個單獨的線程上運行並初始化ui,則必須使用線程併發性,並確保您使用鎖定和條件來同步運行的功能代碼和UI代碼。我在下面的答案第二,確保做一個重建。 – JQluv