2013-05-28 142 views
0

我正在嘗試爲Android創建一個程序。但是,每當我在AVD上運行它時,它會在LogCat中返回一個錯誤,說我的代碼中有一個NullPointerException,因此它「無法實例化活動ComponentInfo」。Android Activity - NullPointerException(「無法實例化活動ComponentInfo」)?

這裏是我的代碼:

package com.example.sensor; 

import android.app.Activity; 
import android.os.Bundle; 
import android.hardware.SensorManager; 
import android.hardware.Sensor; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorEvent; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private SensorManager sensorMgr; 
    private TextView result; 
    Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    private float x, y, z; 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     result = (TextView)findViewById(R.id.result); 

     sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE); 
     SensorEventListener lsn = new SensorEventListener() { 
      @SuppressWarnings("deprecation") 
      public void onSensorChanged(SensorEvent e) { 
      float[] values = e.values; 
      x = e.values[SensorManager.DATA_X]; 
      y = e.values[SensorManager.DATA_Y]; 
      z = e.values[SensorManager.DATA_Z]; 
      result.setText("x="+(int)x+","+"y="+(int)y+","+"z="+(int)z); 
      } 
      public void onAccuracyChanged(Sensor s, int accuracy) { 
      } 
     }; 
     sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

這裏是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

這裏是logcat的:

05-28 13:07:49.896: D/AndroidRuntime(8658): Shutting down VM 
05-28 13:07:49.896: W/dalvikvm(8658): threadid=1: thread exiting with uncaught exception (group=0x400205a0) 
05-28 13:07:49.906: E/AndroidRuntime(8658): FATAL EXCEPTION: main 
05-28 13:07:49.906: E/AndroidRuntime(8658): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sensor/com.example.sensor.MainActivity}: java.lang.NullPointerException 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1743) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.os.Looper.loop(Looper.java:150) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread.main(ActivityThread.java:4277) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at dalvik.system.NativeStart.main(Native Method) 
05-28 13:07:49.906: E/AndroidRuntime(8658): Caused by: java.lang.NullPointerException 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at com.example.sensor.MainActivity.<init>(MainActivity.java:39) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at java.lang.Class.newInstanceImpl(Native Method) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at java.lang.Class.newInstance(Class.java:1409) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.Instrumentation.newActivity(Instrumentation.java:1040) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1735) 
05-28 13:07:49.906: E/AndroidRuntime(8658):  ... 11 more 
05-28 13:07:52.018: I/Process(8658): Sending signal. PID: 8658 SIG: 9 
05-28 13:28:55.300: D/AndroidRuntime(8707): Shutting down VM 
05-28 13:28:55.300: W/dalvikvm(8707): threadid=1: thread exiting with uncaught exception (group=0x400205a0) 
05-28 13:28:55.310: E/AndroidRuntime(8707): FATAL EXCEPTION: main 
05-28 13:28:55.310: E/AndroidRuntime(8707): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sensor/com.example.sensor.MainActivity}: java.lang.NullPointerException 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1743) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.os.Looper.loop(Looper.java:150) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread.main(ActivityThread.java:4277) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at dalvik.system.NativeStart.main(Native Method) 
05-28 13:28:55.310: E/AndroidRuntime(8707): Caused by: java.lang.NullPointerException 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at com.example.sensor.MainActivity.<init>(MainActivity.java:39) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at java.lang.Class.newInstanceImpl(Native Method) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at java.lang.Class.newInstance(Class.java:1409) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.Instrumentation.newActivity(Instrumentation.java:1040) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1735) 
05-28 13:28:55.310: E/AndroidRuntime(8707):  ... 11 more 
05-28 13:28:57.262: I/Process(8707): Sending signal. PID: 8707 SIG: 9 

有誰知道什麼可能會造成這個?非常感謝。

+1

請張貼logcat的輸出,你叫'super.onCreate(savedInstanceState);'你的代碼中兩次,檢查太:) – reidzeibel

+0

請顯示日誌貓 – asloob

回答

3

移動Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);裏面onCreatesensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);

+0

非常感謝,它的工作原理。 – Entel

1

你叫

super.onCreate(savedInstanceState); 

方法兩次。

+0

中給出的錯誤,但它不應該導致nullpointerexception ...更好地發佈logcat錯誤日誌 – stinepike

+0

非常感謝! – Entel

1

你缺少這一行。

 sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE); 
     //sensorMgr not initialized. probably gettinh NullPointerException 

還可以使用上述中的onCreate(PARAM)

 protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); // call this once 
     setContentView(R.layout.activity_main); 
     sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE); // initialize 
     Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     result = (TextView)findViewById(R.id.textView1); 
     ..... 
     } 
+0

非常感謝! – Entel

相關問題