Android Activity — NullPointerException (“unable to instantiate activity ComponentInfo”)?

StackOverflow https://stackoverflow.com/questions/16784444

문제

I'm trying to create a program for Android. However, whenever I run it on the AVD, it returns an error in LogCat saying that there is a NullPointerException in my code, and that it is therefore "unable to instantiate activity ComponentInfo".

Here is my code:

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;
    }
}

Here is my 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>

Here is the 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

Does anyone know what could be causing this? Thanks a lot.

도움이 되었습니까?

해결책

Move Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); to inside onCreate after sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);

다른 팁

you have called

super.onCreate(savedInstanceState);

method twice.

You are missing this line.

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

Also use the above in 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); 
        .....
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top