有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序在创建活动时崩溃

我的应用程序在onCreate方法期间崩溃。它使它成为文本敏感2。然后进入ActivityThread类,并在以下行停止:appContext。setOuterContext(活动)。我以为那是我的seekbar,所以我把它取了下来,现在我想它一定是savedInstanceState包。如果可以的话,请帮忙

错误信息在代码下面

package ricciappcom.stepcounter;

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.SeekBar;
import 安卓.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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at 安卓.app.ActivityThread.access$900(ActivityThread.java:170)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:146)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.安卓.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 安卓.app.Activity.performCreate(Activity.java:5585)
            at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at 安卓.app.ActivityThread.access$900(ActivityThread.java:170)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:146)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5635)

共 (1) 个答案

  1. # 1 楼答案

    在onCreate()中,您将获得NullPointerException,可能的候选项包括:

    1. 在seekBar()方法中初始化seekBar,但在onCreate()启动之前从未调用它,因此:

      seekBar.setProgress(10); 
      

    对空对象调用

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

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

    也会导致NullPointerException

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

    Caused by: java.lang.NullPointerException at ricciappcom.stepcounter.Settings.onCreate(Settings.java:39)

    在这种情况下,第39行出现NullPointerException