0

我正在尝试为 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

有谁知道这可能是什么原因造成的?非常感谢。

4

3 回答 3

5

之后移到Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);里面onCreatesensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);

于 2013-05-28T05:31:24.243 回答
1

你打过电话

super.onCreate(savedInstanceState);

方法两次。

于 2013-05-28T05:27:02.313 回答
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); 
        .....
        }
于 2013-05-28T05:31:31.317 回答