سؤال

I have a simple, one class application which should turn on the camera's light but this is my log cat of errors;

02-02 12:40:47.939: W/dalvikvm(24314): threadid=1: thread exiting with uncaught exception (group=0x4178d898)
02-02 12:40:47.949: E/AndroidRuntime(24314): FATAL EXCEPTION: main
02-02 12:40:47.949: E/AndroidRuntime(24314): java.lang.NullPointerException
02-02 12:40:47.949: E/AndroidRuntime(24314):    at de.vogella.android.notificationmanager.NotificationReceiverActivity.onClick(NotificationReceiverActivity.java:53)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.view.View.performClick(View.java:4475)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.view.View$PerformClick.run(View.java:18784)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Handler.handleCallback(Handler.java:730)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Looper.loop(Looper.java:137)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.app.ActivityThread.main(ActivityThread.java:5414)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at java.lang.reflect.Method.invoke(Method.java:525)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at dalvik.system.NativeStart.main(Native Method)

And here is my class;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class NotificationReceiverActivity extends Activity implements OnClickListener {
    private final String CLASSNAME = getClass().getSimpleName();
    boolean camOn = false;
    public boolean started;

Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
started = true;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.result);

wl.acquire();
initialize();
ib1.setOnClickListener(this);
Log.i(CLASSNAME, "CREATING NOW  "+camOn);
}

private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}

public void onClick(View v) {

// TODO Auto-generated method stub
if (camOn == false) {
       cam.release();

    cam = Camera.open();
    para = cam.getParameters();
    para.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(para);
    Log.i(CLASSNAME, "AA1  "+camOn);
    camOn = true;
    Log.i(CLASSNAME, "AA2  "+camOn);

} 



else {
    para.setFlashMode(Parameters.FLASH_MODE_OFF);
    cam.setParameters(para);
    cam.release();
    //cam = null;
    Log.i(CLASSNAME, "BB1  "+camOn);
    camOn = false;
    Log.i(CLASSNAME, "BB2  "+camOn);

}
}

@Override
protected void onPause() {
super.onPause();
// TODO Auto-generated method stub
cam.release();
//  cam=cam;
// finish();*/
}
@Override
protected void onStop() {
super.onStop();
// cam.release();

}
@Override
protected void onDestroy() {
super.onDestroy();
// cam.release();

}
protected void onResume() {
super.onResume();
//cam = Camera.open();
}

}

If I run this same exact code on android 4.1.2, it works flawlessly, but on my android Galaxy note3, which is 4.3, it shows the GUI and when you click on the button to toggle camera light on/off it does nothing, and gives me the log cat as you saw.

Here is the layout file;

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Flash" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="147dp"
        android:layout_marginBottom="15dip"
        android:gravity="center"
        android:padding="15dip"
        android:text="\n\n\n\n\n"
        android:textColor="#3b3b3b"
        android:textSize="13dip" />

    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="15dip"
        android:gravity="center"
        android:padding="15dip"
        android:text="\n"
        android:textColor="#3b3b3b"
        android:textSize="13dip" />

</LinearLayout>
هل كانت مفيدة؟

المحلول

A couple of things I noticed.

First, when camOn == false you have cam.release() even though cam=Null at that point. If I'm missing something there, that's fine.

The second is that you actually need a surfaceView element to use the camera. Some devices are OK without that, but it is part of the android system, as detailed here.

For example, after adding a surfaceView to your layout, you would do

msurfaceView = (SurfaceView)findViewById(R.id.surfaceView);
msurfaceHolder = surfaceView.getHolder();
msurfaceHolder.addCallback(this);
msurfaceHolder.setFixedSize(1, 1); 

and after setting the parameters for the camera with setParameters, call

mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();

This was an issue on one of my phones, and using the surface view solved that. I assuming you've edited the manifest file to include the right permissions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top