【问题标题】:Android back-button causes crash?Android后退按钮导致崩溃?
【发布时间】:2014-08-21 19:45:19
【问题描述】:

我有一个问题,一旦我进入应用程序并按下手机上的“后退按钮”,然后再次进入应用程序,我的应用程序就会不断崩溃。我想我正在处理某种状态或错误:

package com.animeus;

import com.animeus.Factories.CameraDialogsFactory;
import com.animeus.Factories.CameraFactory;
import com.animeus.Services.CameraService;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class LightBulbActivity extends Activity {
    /** Called when the activity is first created. */
    Camera c;

    //Application starts here
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadComponentsToUse();
        setComponentEvents();

        if (c == null)
            CameraDialogsFactory.GetNoCameraFoundDialog(this).show();
        else
            setComponentEvents();
    }

    //Sets all the components events
    private void setComponentEvents() {
        View v = (View)findViewById(R.id.LightBulbView);
        v.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                triggerLightEvent(event.getAction());
                return false;
            }
        });
    }

    //Turns the led on or off
    private void triggerLightEvent(int currentevent) {
        if (currentevent == MotionEvent.ACTION_DOWN)
            CameraService.turnLedLightOn(c);
        else if (currentevent == MotionEvent.ACTION_UP)
        {
            CameraService.turnLedLightOff(c);
        }
    }

    //Loads the "global" components we are supposed to use
    private void loadComponentsToUse() {
        c = CameraFactory.getCamera();
    }

    //Called once the activity ain't visible for the user anymore
    @Override
    public void onStop() {
        super.onStop();
    }


    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onResume() {
        super.onResume();
    }
}

有什么想法吗? 我还尝试在“onPause”和“onStop”上释放相机,然后重新创建相机“onResume”,但这也会导致应用崩溃......

是的,我知道.. 这不是完整的代码.. 但如果您需要更多代码,请告诉我

提前致谢!

【问题讨论】:

  • 不完全确定,但我见过人们在 onPause 事件中丢弃相机。这取决于你弄清楚android是如何处理未处理的东西的:p
  • 你有堆栈跟踪或一些日志吗?

标签: android crash android-camera


【解决方案1】:

当用户点击后退按钮时,您希望执行什么样的操作?在我的应用程序中,如果用户在主要活动中并且不在活动堆栈深处,我会覆盖后退按钮以提示用户 AlertDialog 询问他们是否要退出应用程序,然后如果他们按下是我使用System.exit() 在我打扫房子之后,可以说我创造了一种首先杀死和消除一切的方法。

你可以像这样覆盖后退按钮:

@Override
public void onBackPressed(){
    super.onBackPressed();
    cleanHouse();
    System.exit(0);     
}

你也可以把它放在你的onPause():

@Override
protected void onPause() {
    super.onPause();
    try{
        cam.camera.release();
    }catch(NullPointerException e){
        e.printStackTrace();
        try{
            cam.camera.unlock();
        }catch(NullPointerException f){
            f.printStackTrace();
        }
    }
}

【讨论】:

  • 嗯,我不认为实际的问题是后退按钮的操作更像是当用户“重新打开”应用程序时......那就是崩溃发生的时候......
  • 但我想如果你使用 system.exit() 它应该杀死应用程序的状态,就像它还没有启动一样,所以所有视图都将设置为再次通过 oncreate .....将有助于解决您在使用相机时遇到的问题,但它可以防止崩溃在 onresume 中发生。我将添加一个小例子,说明我在 onPause 中使用什么来释放相机并捕获错误。
  • 如果这些不适合你,那么你应该在问题中添加一个 logcat 转储,以便我们可以看到错误堆栈
猜你喜欢
  • 2013-09-17
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多