android instant错误:无法实例化活动ComponentInfo

时间:2013-05-19 19:18:55

标签: android

我想要帮助,我找不到问题。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alnassre.prayingobserver"
    android:versionCode="2"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />
    <uses-feature android:name="android.hardware.sensor.light" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.alnassre.prayingobserver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

package com.alnassre.prayingobserver;


import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

    private SensorManager m_sensor_manager;
    private  Sensor m_accelerometer;


    final Button button = (Button) findViewById(R.id.btn1);
    final TextView txt_rkoa = (TextView) findViewById(R.id.textView1);
    final TextView txt_sjod = (TextView) findViewById(R.id.textView2);


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_LIGHT);
        setContentView(R.layout.activity_main);




        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                int tkoa = Integer.parseInt(txt_rkoa.getText().toString());
                tkoa++;
                txt_rkoa.setText(String.valueOf(tkoa));

            }
        });

    }

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

    protected void onResume()
    {
        super.onResume();
        m_sensor_manager.registerListener(this, m_accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onSensorChanged(SensorEvent event)
    {
        _("onSensorChanged");
        _("" + event.values[0]);

       // Toast.makeText(this, "onSensorChanged "+ event.values[0], Toast.LENGTH_SHORT).show();
        Log.i("onSensorChanged ", event.values[0]+"");
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
        Toast.makeText(this, "onAccuracyChanged", Toast.LENGTH_SHORT).show();

    }

    private void _(String msg)
    {
        Log.d("chovanec", msg);
    }

}

错误陈述:

05-19 22:13:24.664: E/AndroidRuntime(12565): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.alnassre.prayingobserver/com.alnassre.prayingobserver.MainActivity}: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

首先应将setContent设置为活动,然后通过视图层次结构的findviewbyid初始化视图。否则你会得到NullPointerException

 private SensorManager m_sensor_manager;
 private  Sensor m_accelerometer;
 Button button;
 TextView txt_rkoa,txt_sjod;     
 @Override
 protected void onCreate(Bundle savedInstanceState)
 { 
 super.onCreate(savedInstanceState);
 m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
 m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_LIGHT);
 setContentView(R.layout.activity_main);
 button = (Button) findViewById(R.id.btn1);
 txt_rkoa = (TextView) findViewById(R.id.textView1);
 txt_sjod = (TextView) findViewById(R.id.textView2);
 .....
 }

答案 1 :(得分:0)

您的错误发生在:

final Button button = (Button) findViewById(R.id.btn1);
final TextView txt_rkoa = (TextView) findViewById(R.id.textView1);
final TextView txt_sjod = (TextView) findViewById(R.id.textView2);

您正试图在调用onCreate之前找到视图以设置内容setContentView(R.layout.activity_main);

应该更像是:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_LIGHT);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.btn1);
    txt_rkoa = (TextView) findViewById(R.id.textView1);
    txt_sjod = (TextView) findViewById(R.id.textView2);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            int tkoa = Integer.parseInt(txt_rkoa.getText().toString());
            tkoa++;
            txt_rkoa.setText(String.valueOf(tkoa));

        }
    });
}

同样在将来,通过整个跟踪堆栈,告诉你你失败的是什么(NullPointerException)