繁体   English   中英

应用程序在 onCreate 活动期间崩溃

[英]app crashes during onCreate of activity

我的应用程序在 onCreate 方法期间崩溃。 它进入 textSensitive2.setText,然后进入 ActivityThread 类并停止在线:appContext.setOuterContext(Activity)。 我以为是我的seekbar所以我删除了它,现在我认为它必须是Bundle savedInstanceState。 如果可以的话请帮忙。

错误信息在代码下方

package ricciappcom.stepcounter;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;

public class Settings extends Activity implements SeekBar.OnSeekBarChangeListener {

    private TextView textViewX;
    private TextView textViewY;
    private TextView textViewZ;


 private SensorManager sensorManager;
    private float acceleration;
    private int threshold;
    private float prev;
    private float cur;
    private int numSteps;
    private SeekBar seekBar;
    private TextView textSensitive;
    private TextView textSteps;
    public void Seekbar(){
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textSensitive.setText(String.valueOf(progress));
                threshold = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                textSensitive.setText(String.valueOf(threshold));
                MainActivity.threshold = threshold;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        textViewX = (TextView)findViewById(R.id.textViewX);
        textViewY = (TextView)findViewById(R.id.textViewY);
        textViewZ = (TextView)findViewById(R.id.textViewZ);
        seekBar.setProgress(10);
        threshold = 10;
        textSensitive.setText(String.valueOf(threshold));
        prev = 0;
        cur = 0;
        acceleration = 0.00f;
        enableAccelerometerListening();

    }

    private void enableAccelerometerListening() {
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }
    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            cur = y;

            if (Math.abs(cur - prev) > threshold) {
                numSteps++;
                textSteps.setText(String.valueOf(numSteps));
            }
            prev = y;
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }

    };
        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
    public void reset() {


    }
}


11-29 14:20:45.191  26144-26144/ricciappcom.stepcounter W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418a7da0)
11-29 14:20:45.191  26144-26144/ricciappcom.stepcounter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ricciappcom.stepcounter, PID: 26144
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ricciappcom.stepcounter/ricciappcom.stepcounter.Settings}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at ricciappcom.stepcounter.Settings.onCreate(Settings.java:39)
            at android.app.Activity.performCreate(Activity.java:5585)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)

你在 onCreate() 中得到NullPointerException ,可能的候选者:

  1. 您在 Seekbar() 方法中初始化了 seekBar,但它在 onCreate() 开始之前从未被调用,因此:

     seekBar.setProgress(10);

在空对象上调用。

  1. 稍后在 onCreate() 中,您在 textSensitive TextView 上调用 setText(),但同样,在提供的代码中,您实际上没有使用 findViewById() 对其进行初始化,因此

    textSensitive.setText(String.valueOf(threshold));

也会导致 NullPointerException。

确保您初始化了 TextView 和 ProgressBar,另外,请阅读 logcat,它包含出现问题的确切位置,例如。

引起:在 ricciappcom.stepcounter.Settings.onCreate(Settings.java:39) 的 java.lang.NullPointerException

在这种情况下,第 39 行发生 NullPointerException...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM