我正在为Nexus& amp;开发FlashLight应用程序星系装置。 该应用程序适用于Galaxy,但有一件事是错误的。 当我将应用程序设置为后台(通过按主页按钮)然后单击小部件时,它会从mainactivity打开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='ClientState{4366a1d0 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();”当我点击Widget时。
@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();
}
}
}
}
Receiver,调用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;
}
}
}
答案 0 :(得分: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):引起:java.lang.RuntimeException:无法连接到摄像头服务