我正在尝试为Android创建一个程序。但是,无论何时我在AVD上运行它,它都会在LogCat中返回一个错误,说我的代码中存在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
有谁知道造成这种情况的原因是什么?非常感谢。
答案 0 :(得分:4)
在Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
onCreate
移至sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
内
答案 1 :(得分:1)
super.onCreate(savedInstanceState);
方法两次。
答案 2 :(得分: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);
.....
}