1

I want help in this, I can't find out the problem.

<?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>

and

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

}

Error statement:

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
4

2 回答 2

0

您应该首先将内容设置为活动,然后通过视图层次结构的 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);
 .....
 }
于 2013-05-19T19:21:49.840 回答
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) 上失败的原因

于 2013-05-19T19:22:13.630 回答