Android:onResume() 正在跳转到除异常之外的另一个活动

Posted

技术标签:

【中文标题】Android:onResume() 正在跳转到除异常之外的另一个活动【英文标题】:Android: onResume() is jumping to another activity than excepted 【发布时间】:2013-12-01 18:19:03 【问题描述】:

我正在为 Nexus 和 Galaxy 设备开发 FlashLight 应用程序。 该应用程序可以在 Galaxy 上运行,但有一件事出了问题。 当我将应用程序设置为后台(通过按主页按钮)然后单击小部件时,它会从 mainactivity 中打开 onResume,如下所示:

所以我的问题是:

    如何检查活动(在本例中为 FlashLight.java)是否正在运行? 如何从 invisible_activity.java 中终止正在运行的活动? 为什么会调用 FlashLight.java 中的 onResume(),我该如何控制这个问题?

红场的LogCat:

    11-18 15:36:03.300: D/Camera(9781): app passed NULL surface
11-18 15:36:03.400: D/InputTransport(9781): Input channel constructed: name='ClientState4366a1d0 uid 10141 pid 9781 (server)', fd=56
11-18 15:36:03.445: D/androidRuntime(9781): Shutting down VM
11-18 15:36:03.445: W/dalvikvm(9781): threadid=1: thread exiting with uncaught exception (group=0x41d26700)
11-18 15:36:03.460: E/AndroidRuntime(9781): FATAL EXCEPTION: main
11-18 15:36:03.460: E/AndroidRuntime(9781): java.lang.RuntimeException: Unable to resume activity spicysoftware.spicytaschenlampe/spicysoftware.spicytaschenlampe.FlashLight: java.lang.RuntimeException: Fail to connect to camera service
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2918)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2947)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.os.Looper.loop(Looper.java:137)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.main(ActivityThread.java:5415)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invoke(Method.java:525)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at dalvik.system.NativeStart.main(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781): Caused by: java.lang.RuntimeException: Fail to connect to camera service
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.native_setup(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.<init>(Camera.java:413)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.open(Camera.java:384)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at spicysoftware.spicytaschenlampe.FlashLight.check_for_flash(FlashLight.java:82)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at spicysoftware.spicytaschenlampe.FlashLight.onResume(FlashLight.java:74)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.Activity.performResume(Activity.java:5450)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2908)
11-18 15:36:03.460: E/AndroidRuntime(9781):     ... 10 more

来自 FlashLight.java 的 onResume()

它在“cam = Camera.open();”处崩溃当我点击小部件时。

@Override
protected void onResume()
 super.onResume();

 check_settings();

 if(light_ == 0)
        finish();
        Intent i_screenlight = new Intent(FlashLight.this, ScreenLight_noFlash.class);  
        startActivity(i_screenlight); 
 else
 mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    if(cam != null)

     else 
        rLayout = (RelativeLayout) findViewById (R.id.layout_background);       
        cam = Camera.open();  //===> It's crashing here, when I open the widget     
        cam.startPreview();
        p = cam.getParameters();

        if(light == 1)     
            if(sound == 0)
                mPlayer.start();   
            
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cmd_flash_light.setText(R.string.off);
                cmd_flash_light.setBackgroundResource(R.drawable.ein_2);
                cam.setParameters(p);
                cam.startPreview();

         else 

            if(sound == 0)
                mPlayer.start(); 
            
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cmd_flash_light.setText(R.string.on);
                cmd_flash_light.setBackgroundResource(R.drawable.ein);
                cam.setParameters(p);           
                cam.startPreview();
        
    
 

接收者,调用 invisible_activity.java

@Override
public void onReceive(Context context, Intent intent) 

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flash_widget);

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context,     FlashlightWidgetProvider.class),
                                                                         views);
        Intent i = new Intent();
        i.setClassName("spicysoftware.spicytaschenlampe", "spicysoftware.spicytaschenlampe.invisible_activity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);       

invisible_activity.java(用于处理 LED 灯的小部件)

public class invisible_activity extends Activity implements SurfaceHolder.Callback
    private static Camera cam_;
    SurfaceView preview_;
    SurfaceHolder mHolder;
    shell_preferences shellpreference = new shell_preferences(this);
    int isLightOn;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.invisible);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        isLightOn = shellpreference.getInt("LIGHT_ACT", 0);
         if (isLightOn == 1) 
             if (cam_ != null) 
                     cam_.stopPreview();
                     cam_.release();
                     cam_ = null;
                     finish();
             
             shellpreference.setInt("LIGHT_ACT", 0);

      else 
             // Open the default i.e. the first rear facing cam_.
             cam_ = Camera.open();

                     // Set the torch flash mode
                     Parameters param = cam_.getParameters();
                     param.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    preview_ = (SurfaceView) findViewById(R.id.surfaceView2);
                    mHolder = preview_.getHolder();
                    mHolder.addCallback(this);

                    try            
                        cam_.setPreviewDisplay(mHolder);
                     catch (IOException e) 
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    

                        cam_.setParameters(param);
                        cam_.startPreview();
                             shellpreference.setInt("LIGHT_ACT", 1);
                                finish();
     

    

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) 
        // TODO Auto-generated method stub

    

    public void surfaceCreated(SurfaceHolder holder) 
        // TODO Auto-generated method stub
        mHolder = holder;
        try 
            cam_.setPreviewDisplay(mHolder);
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

    public void surfaceDestroyed(SurfaceHolder holder) 
        // TODO Auto-generated method stub
        if(cam_ != null)
         cam_.stopPreview();
            mHolder = null;

        
    


【问题讨论】:

【参考方案1】:

我认为您在实际销毁您的应用程序/活动时忘记处理释放相机。

尝试覆盖 onDestroy():

@Override
protected void onDestroy() 
    super.onDestroy();
    if (camera!=null)
    
        camera.stopPreview();
        camera.release();
        camera=null;
   

至少你的 logcat 指出了这个问题:

11-18 15:36:03.460: E/AndroidRuntime(9781): Caused by: java.lang.RuntimeException: 无法连接到相机服务

【讨论】:

试试这个:在您收到上述错误后,尝试在您的设备上加载标准相机应用程序并检查它是否可以连接。如果没有,你必须升级你的相机释放肯定! 所以你的意思是我应该模拟错误,然后打开相机应用程序? -> 这是有效的,还是我应该在错误出现之前以编程方式打开相机应用程序? -> 完全一样的错误 不,我指的是您手机上已有的标准相机应用程序。这个想法是同时只能存在一个使用相机的应用程序。所以基本上当您的自定义应用程序没有正确释放相机时,另一个相机应用程序无法连接到相机。这就是我想给你看的!

以上是关于Android:onResume() 正在跳转到除异常之外的另一个活动的主要内容,如果未能解决你的问题,请参考以下文章

如何从activity页面跳转到fragment页面

Android View onResume 方法?

android 生命周期

Android onNewIntent调用时机

Android中当前Activity跳转到当前Activity页面

如何跳转到 Android 测试应用程序的下一部分?