-1

keep getting this error don't know why every thing is perfect i think . if any help here

04-10 22:54:25.753  28129-28129/com.ahmed.raja.wrongclick E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ahmed.raja.wrongclick, PID: 28129
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmed.raja.wrongclick/com.ahmed.raja.wrongclick.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.ahmed.raja.wrongclick.MainActivity.onCreate(MainActivity.java:81)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

here is my MainActivity i think there is an issue oncreate method just can't find out which thing is crashing my app . I have double checked my XML files

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by DELL on 4/10/2015.
 */
public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private final static String LOG_TAG = "DevicePolicyAdmin";
    DevicePolicyManager dpm;
    ComponentName DevicePolicyAdmin;
    private CheckBox truitonAdminEnabledCheckbox;
    protected static final int REQUEST_ENABLE = 1;
    Button btntake;


    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        DevicePolicyAdmin = new ComponentName(this,
                Dprcv.class);

        btntake =(Button)findViewById(R.id.takepicture);
        truitonAdminEnabledCheckbox = (CheckBox) findViewById(R.id.checkBox);
//start



    //    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        ViewGroup.LayoutParams layoutParamsControl
                = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);
btntake.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        camera.takePicture(myShutterCallback,
                myPictureCallback_RAW, myPictureCallback_JPG);
    }
});












//end

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (isMyDevicePolicyReceiverActive()) {
            truitonAdminEnabledCheckbox.setChecked(true);
        } else {
            truitonAdminEnabledCheckbox.setChecked(false);
        }
        truitonAdminEnabledCheckbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        if (isChecked) {
                            Intent intent = new Intent(
                                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                            intent.putExtra(
                                    DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                                    DevicePolicyAdmin);
                            intent.putExtra(
                                    DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                                    getString(R.string.wow));
                            startActivityForResult(intent, REQUEST_ENABLE);
                        } else {
                            dpm
                                    .removeActiveAdmin(DevicePolicyAdmin);
                        }
                    }
                });
    }

    private boolean isMyDevicePolicyReceiverActive() {
        return dpm
                .isAdminActive(DevicePolicyAdmin);
    }




    Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }};

    Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

        }};

    Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

  /*Bitmap bitmapPicture

   = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */



            Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());



            OutputStream imageFileOS;

            try {

                imageFileOS = getContentResolver().openOutputStream(uriTarget);

                imageFileOS.write(arg0);

                imageFileOS.flush();

                imageFileOS.close();



                Toast.makeText(MainActivity.this,

                        "waH kiAa selfie hAi :D ",
                        //+ uriTarget.toString(),

                        Toast.LENGTH_LONG).show();



            } catch (FileNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }
            camera.startPreview();
        }};
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        //  camera = Camera.open();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Log.d("No of cameras", Camera.getNumberOfCameras() + "");
        for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
            Camera.CameraInfo camInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(camNo, camInfo);

            if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
                camera = Camera.open(camNo);
            }
        }
        if (camera == null) {
            // no front-facing camera, use the first back-facing camera instead.
            // you may instead wish to inform the user of an error here...
            camera = Camera.open();
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

















}
Brendom
  • 123
  • 1
  • 10

1 Answers1

0

You do not seem to have initialized your camera instance before you call the following line while setting the click listener for Button btnTake:

camera.takePicture(myShutterCallback,
            myPictureCallback_RAW, myPictureCallback_JPG);

Hence you are getting the NullPointerException.

You need to initialize your camera instance before using it. I see you are doing it in the surfaceCreated() method but that is called after onCreate() and hence the Camera instance resolves to null.

Have the following code somewhere in the onCreate() method before initializing you set the click listener for the button.

try {
    c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
    // Camera is not available (in use or does not exist)
}
ucsunil
  • 7,378
  • 1
  • 27
  • 32