在设备上运行apk时出错(Android)

时间:2012-12-25 14:11:18

标签: android eclipse

我不知道为什么应用程序仅在设备的进程调试中运行。当我尝试在设备上使用该应用程序时,该应用程序无法正常工作。我在下面显示错误。

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);
    }   

}

没有想法来解决这个问题: - (

1 个答案:

答案 0 :(得分:0)

由于svc字段仍为null,因此活动停止时会出现问题。您在run方法中的代码可能应该使用onCreate方法。