2

Im getting the following crash report

Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3322 (has extras) }} to activity {com.example.instareport/com.example.instareport.MainActivity}: java.lang.NullPointerException

My fragment_main.xml looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.instareport.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:visibility="visible"
        android:id="@+id/imageView1" />
</RelativeLayout>

and my MainActivity class is below

public class MainActivity extends Activity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mImageView;
    private Bitmap  imageBitmap;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            super.onActivityResult(requestCode, resultCode, data);
            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView)findViewById(R.id.imageView1);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        dispatchTakePictureIntent();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

Basically, whent the app launches, it should call the Camera App since there is an intent delievered to it,, and when I press the shutter button, I press Ok (to accept the image), and then it starts to crash.

I tried inserting breakpoint at the following line,, and this is where the code throws an exception

mImageView.setImageBitmap(imageBitmap);

Why is that so? I tried to follow the Android tutorials from both the official Android website and Android tutorial's point.

Thanks

edited.

here is the logcat

07-17 21:16:12.131: W/dalvikvm(21693): threadid=1: thread exiting with uncaught exception (group=0x41522438)
07-17 21:16:12.191: E/AndroidRuntime(21693): FATAL EXCEPTION: main
07-17 21:16:12.191: E/AndroidRuntime(21693): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3322 (has extras) }} to activity {com.example.instareport/com.example.instareport.MainActivity}: java.lang.NullPointerException
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread.access$1100(ActivityThread.java:138)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.os.Looper.loop(Looper.java:137)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread.main(ActivityThread.java:4911)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at dalvik.system.NativeStart.main(Native Method)
07-17 21:16:12.191: E/AndroidRuntime(21693): Caused by: java.lang.NullPointerException
07-17 21:16:12.191: E/AndroidRuntime(21693):    at com.example.instareport.MainActivity.onActivityResult(MainActivity.java:66)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.Activity.dispatchActivityResult(Activity.java:5492)
07-17 21:16:12.191: E/AndroidRuntime(21693):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
07-17 21:16:12.191: E/AndroidRuntime(21693):    ... 11 more
vvavepacket
  • 1,882
  • 4
  • 25
  • 38
  • What permissions do you have configured? See here: http://developer.android.com/guide/topics/manifest/uses-permission-element.html – Rai Jul 18 '14 at 01:35
  • which is line 66 in your activity? – Anil Jadhav Jul 18 '14 at 01:49
  • Try this first to see if the camera is available: http://stackoverflow.com/a/5266165/1990536 – Rai Jul 18 '14 at 02:16
  • Actually is the NPE happening on the object reference being null or is it thrown from the method call? I have not run this code. – Rai Jul 18 '14 at 02:42
  • Here is the complete source code so you can emulate the exception on your end. Thanks! http://www.mediafire.com/download/58znjqcmrnb577p/instaReport.zip – vvavepacket Jul 18 '14 at 04:10

3 Answers3

1
public class MainActivity extends Activity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mImageView;
    private Bitmap imageBitmap;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            super.onActivityResult(requestCode, resultCode, data);
            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            // imageBitmap = (Bitmap) data.getExtras().get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        dispatchTakePictureIntent();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



}

Delete fragment_main.xml. And edit activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.instareport.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

If the bitmap you retrieved is acceptable then just do it this way. If it's size is too small you can get a better solution here http://pastebin.com/NF9yu0sa

Huy Tran
  • 208
  • 1
  • 8
  • Ok. I'll continue looking – Huy Tran Jul 18 '14 at 04:03
  • if this is of any help , here is the complete source code. http://www.mediafire.com/download/58znjqcmrnb577p/instaReport.zip – vvavepacket Jul 18 '14 at 04:09
  • Ok, I'll check. Thanks. – Huy Tran Jul 18 '14 at 04:12
  • I still haven't found the problem. But I have alternative solution for setting the captured image to the ImageView. Would that be ok? – Huy Tran Jul 18 '14 at 04:20
  • Still your old method, just fixed some places. It's working now. However the bitmap you got using this method is resized which makes the quality kinda bad (this happens on my device). – Huy Tran Jul 18 '14 at 04:59
  • ok, Ill study the code. Ill inform you after Im finished. Thanks – vvavepacket Jul 18 '14 at 05:20
  • It works now,, hmmm why did we delete the fragment part? Is it because, I forgot to instantiate the mImageView in the fragment part? (I instantiated it in the main window?) – vvavepacket Jul 18 '14 at 08:01
  • Not sure why though, I have just started learning Android recently, and my instructor always delete `fragment_main.xml` whenever he creates a new project for our lessons – Huy Tran Jul 18 '14 at 08:23
0

Have you determined whether it's data or extras that's null? (Do a quick check, if not). Also, have you tried it without the "super" line?

Sorry I can't yet do comments.

Sophicles
  • 64
  • 8
0

Th reason of NullPointer Exception is that you didn't initialize Bitmap varible. Your Code may be like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mImageView = (ImageView)findViewById(R.id.imageView1);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.imageView1);
    ****Rest of your code ********
    }
  • he is getting the bitmap from the gallery or somewhere, why he need to use ur above code ? which make no sense to me, its his data that return null. – Kosh Jul 18 '14 at 02:38