2012-12-25 38 views
0

我不知道爲什麼應用程序只在進行設備調試時運行。當我嘗試在設備上使用應用程序時,該應用程序無法正常工作。我在下面顯示錯誤。在設備上運行apk時出錯(Android)

12-25 11:58:51.829: E/Trace(30281): error opening trace file: No such file or directory (2) 
12-25 11:58:52.354: E/AndroidRuntime(30281): FATAL EXCEPTION: main 
12-25 11:58:52.354: E/AndroidRuntime(30281): java.lang.RuntimeException: Unable to stop activity {com.example.menu/com.example.menu.MainActivity}: java.lang.NullPointerException 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3278) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3332) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3530) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.access$700(ActivityThread.java:140) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.os.Looper.loop(Looper.java:137) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.main(ActivityThread.java:4898) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at java.lang.reflect.Method.invokeNative(Native Method) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at java.lang.reflect.Method.invoke(Method.java:511) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at dalvik.system.NativeStart.main(Native Method) 
12-25 11:58:52.354: E/AndroidRuntime(30281): Caused by: java.lang.NullPointerException 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ContextImpl.stopService(ContextImpl.java:1319) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.content.ContextWrapper.stopService(ContextWrapper.java:395) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at com.example.menu.MainActivity.onStop(MainActivity.java:125) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1208) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.Activity.performStop(Activity.java:5322) 
12-25 11:58:52.354: E/AndroidRuntime(30281): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3273) 
12-25 11:58:52.354: E/AndroidRuntime(30281): ... 12 more 

的代碼是:

public class MainActivity extends Activity implements OnTouchListener, Runnable, OnSeekBarChangeListener { 

    private Intent svc; 
    private MediaPlayer mp; 
    private SeekBar sb; 
    private AudioManager am; 

    int Volume=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     setContentView(R.layout.splash); 

     mp = MediaPlayer.create(getBaseContext(), R.raw.spalshduck); 
     mp.start(); 

     Handler handler = new Handler(); 
     handler.postDelayed(this, 3000); 




    } 
    public void addListenerOnButton() { 

     ImageButton soundButton = (ImageButton) findViewById(R.id.sound); 
     soundButton.setOnTouchListener(this); 

    } 
    public boolean onTouch(View v, MotionEvent event) 
    { 

     switch (v.getId()) { 
     case R.id.sound: 
     { 
      setContentView(R.layout.soundview); 
      ImageButton soundButtonback = (ImageButton) findViewById(R.id.soundback); 

      sb =(SeekBar) findViewById(R.id.seekBar1); 

      am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

      mp.start(); 

      int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
      int curVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC); 

      sb.setMax(maxVolume); 
      sb.setProgress(curVolume); 

      sb.setOnSeekBarChangeListener(this); 

      soundButtonback.setOnTouchListener(this); 


      break; 
     } 

     case R.id.soundback: 
     { 
      setContentView(R.layout.activity_main); 
      ImageButton soundButton = (ImageButton) findViewById(R.id.sound); 
      soundButton.setOnTouchListener(this); 
      break; 
     } 
     } 

     return true; 

    } 


    public void onProgressChanged(SeekBar seekb, int progress, boolean arg2) { 
     am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); 
     Volume = progress;  
    } 

    public void onStartTrackingTouch(SeekBar arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onStopTrackingTouch(SeekBar arg0) { 
     // TODO Auto-generated method stub 
     //Toast.makeText(getApplicationContext(), "Volume: " + Integer.toString(Volume), Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 

     return true; 
    } 

    public void finish() { 
     stopService(svc); 
     super.finish(); 
    } 

    public void onStop() { 
     stopService(svc); 
     super.onStop(); 



    } 

    @Override 
    public void run() { 


    setContentView(R.layout.activity_main); 


    svc=new Intent(this, MusicService.class); 
    startService(svc); 


    addListenerOnButton(); 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     setContentView(R.layout.activity_main); 
    } 

} 

沒有IDEIA來解決這個問題:-(

+2

在停止之前檢查'if(svc!= null)'服務 –

回答

0

的問題時,該活動已停止,因爲svc領域仍然null發生在你的代碼在run方法中應該可能進入onCreate方法。

+0

不完全。你可以經過許多onStart()/ onStop()循環,而不會觸及另一個onCreate(),所以對於服務啓動和停止的處理需要在活動生命週期的潛在循環方面有些對稱,儘管你可能能夠先創建Intent對象並保持它。 –

相關問題