2015-11-29 37 views
-1

我的應用程序在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) 

回答

1

你得到NullPointerException異常中的onCreate()下面,有可能的候選人:

  1. 您初始化搜索欄的搜索欄()方法,但它在onCreate()啓動之前從未被調用,因此:

    seekBar.setProgress(10); 
    

在空對象上被調用。

  • 中的onCreate(),你呼籲textSensitive的TextView的setText(),但後來再次,無處所提供的代碼,你實際上是用findViewById初始化這個()因此

    textSensitive.setText(String.valueOf(threshold)); 
    
  • 也導致空指針異常。

    請確保您初始化您的TextView和ProgressBar,並且還請閱讀logcat,它包含問題發生的確切位置,例如。

    產生的原因:在ricciappcom.stepcounter.Settings.onCreate顯示java.lang.NullPointerException (Settings.java:39)

    在這種情況下,空指針異常第39行上發生...

    +0

    非常感謝你這是問題所在。 –