-1

I have a activity in which there is a button which opens camera application then I will try to crop. But my activity doesnt even start. It is probably something about the button, the crash occurs after setonclicklistener. could you please help?

here is my MainActivityCrop.java:

public class MainActivityCrop extends Activity {

    //keep track of camera capture intent
    final int CAMERA_CAPTURE = 1;
    //captured picture uri
    private Uri picUri;
    //keep track of cropping intent
    final int PIC_CROP = 2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("onResume: ", " BB");
        setContentView(R.layout.activity_main_activity_crop);
        Log.d("onResume: ", " CC");
        //retrieve a reference to the UI button
        Button captureBtn = (Button)findViewById(R.id.capture_btn);
        //handle button clicks
        Log.d("onResume: ", " DD");
        captureBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Log.d("onResume: ", " EE");
                    try {
                        Log.d("onResume: ", " FF");
                        //use standard intent to capture an image
                        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        //we will handle the returned data in onActivityResult
                        startActivityForResult(captureIntent, CAMERA_CAPTURE);
                    }
                    catch(ActivityNotFoundException anfe){
                        //display an error message
                        String errorMessage = "Whoops - your device doesn't support capturing images!";
                        Toast toast = Toast.makeText(null, errorMessage, Toast.LENGTH_SHORT);
                        toast.show();
                    }
            }
        });

        }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            //user is returning from capturing an image using the camera
            if(requestCode == CAMERA_CAPTURE){
                //get the Uri for the captured image
                picUri = data.getData(); 
                //carry out the crop operation
                performCrop();
            }
            //user is returning from cropping the image
            else if(requestCode == PIC_CROP){
                //get the returned data
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap thePic = extras.getParcelable("data");
                //retrieve a reference to the ImageView
                ImageView picView = (ImageView)findViewById(R.id.picture);
                //display the returned cropped image
                picView.setImageBitmap(thePic);
            }
        }
    }

    private void performCrop(){
        try {
            //call the standard crop action intent (the user device may not support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
                //indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
                //set crop properties
            cropIntent.putExtra("crop", "true");
                //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
                //indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
                //retrieve data on return
            cropIntent.putExtra("return-data", true);
                //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        }
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

}

Here is my fragment_main_activity_crop.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<Button
    android:id="@+id/capture_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capture" />

<ImageView
    android:id="@+id/picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/picture"
    android:layout_margin="5dp"

    />

</LinearLayout>

These are logcat scripts:

06-03 16:44:26.710: E/AndroidRuntime(5036): FATAL EXCEPTION: main
06-03 16:44:26.710: E/AndroidRuntime(5036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.croptry/com.example.croptry.MainActivityCrop}: java.lang.NullPointerException
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1978)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2003)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.os.Looper.loop(Looper.java:137)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread.main(ActivityThread.java:4446)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at java.lang.reflect.Method.invoke(Method.java:511)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at dalvik.system.NativeStart.main(Native Method)
06-03 16:44:26.710: E/AndroidRuntime(5036): Caused by: java.lang.NullPointerException
06-03 16:44:26.710: E/AndroidRuntime(5036):     at com.example.croptry.MainActivityCrop.onCreate(MainActivityCrop.java:37)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.Activity.performCreate(Activity.java:4465)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-03 16:44:26.710: E/AndroidRuntime(5036):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1942)
Skynet
  • 7,820
  • 5
  • 44
  • 80

2 Answers2

1

I cannot see the error if exception occurs on

captureBtn.setOnClickListener...

Try to clean project and rebuild it and see what happens after. Sometimes it helps.

EDIT:

You use:

setContentView(R.layout.activity_main_activity_crop);

and your button is in

fragment_main_activity_crop.xml

That is reason for error

Zoran
  • 1,484
  • 1
  • 10
  • 13
0

It's because of your camera. try to open camera from simulator and you will see that you cant access it.

  1. You can make .apk file and test it in your real phone.
  2. set camera of simulator as emulated.

I think it will help you.

Karo
  • 273
  • 4
  • 16
  • but I am already trying to run this on my device and it stops the app before even starting. other than that camera app works without any problem. – squareroot Jun 03 '14 at 14:13