我的应用在onCreate方法中崩溃了。它进入textSensitive2.setText,然后进入ActivityThread类并在行上停止:appContext.setOuterContext(Activity)。我以为这是我的搜索栏所以我删除它,现在我认为它必须是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)
答案 0 :(得分:1)
你在onCreate()中得到 NullPointerException ,可能有候选者:
你在Seekbar()方法中初始化了seekBar,但是在onCreate()启动之前它从未被调用过,因此:
seekBar.setProgress(10);
。
稍后在onCreate()中,你在textSensitive TextView上调用setText(),但是在提供的代码中没有任何地方你用findViewById()实际初始化它,因此
textSensitive.setText(String.valueOf(threshold));
也会导致NullPointerException。
确保初始化TextView和ProgressBar,另外,请阅读logcat,它包含问题发生的确切位置,例如。
引起:java.lang.NullPointerException at ricciappcom.stepcounter.Settings.onCreate(Settings.java:39)
在这种情况下,NullPointerException发生在第39行......