1

I have the following scenario which is quite pulling my hairs out: I'm writing a class to manage the Android camera, which extends Activity. Inside that class I have the code below. What happens is that I always get null point exception --with its force close little friend-- in the line that says:

startActivityForResult(intent, 0);

However I get System.out.println("intent not null"); printed ok in LogCat...

The LogCat says:

03-08 22:46:38.584: ERROR/AndroidRuntime(1079): java.lang.NullPointerException
03-08 22:46:38.584: ERROR/AndroidRuntime(1079):     at android.app.Activity.startActivityForResult(Activity.java:2749)
03-08 22:46:38.584: ERROR/AndroidRuntime(1079):     at com.test.cameratest.startCameraActivity(cameratest.java:39)

In the manifest I have:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<activity android:name=".cameratest" />

So, what I'm doing wrong?? Why startActivityForResult always throws null!?

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

/** As per suggestion tried to change the method from protected to private or public
but still have the same problem */
protected void startCameraActivity()
{
   String path = (new StringBuilder()).append(Environment.getExternalStorageDirectory()).append("/images/test.jpg").toString();

    System.out.println("started");

    File file = new File(path);

    System.out.println(path);

    Uri outputFileUri = Uri.fromFile(file);

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

    intent.putExtra("output", outputFileUri);
    /** Removed as per suggestion 
    if (intent!=null)
    {
        System.out.println("intent not null");
            **startActivityForResult(intent, 0);**
    }
    */    

    **startActivityForResult(intent, 0);** <== null pointer exception
}

EDITED:

As per suggestion, I took a look to line 2749 on Activity.java from Android 2.1r2 framework and here is the code:

public void startActivityForResult(Intent intent, int requestCode) {
        if (mParent == null) {

               **** Line 2749 is the folowing                
               Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode);
               ****

            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                // If this start is requesting a result, we can avoid making
                // the activity visible until the result is received.  Setting
                // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
                // activity hidden during this time, to avoid flickering.
                // This can only be done when a result is requested because
                // that guarantees we will get information back when the
                // activity is finished, no matter what happens to it.
                mStartedActivity = true;
            }
        } else {
            mParent.startActivityFromChild(this, intent, requestCode);
        }
    }

Line 2749 is marked with double ** However, I don't see a problem with that code

EDITED

Forgot to explain how I call this:

cameratest cam = new cameratest();
cam.startCameraActivity();
Aram Alvarez
  • 536
  • 6
  • 21
  • Since you got past `intent.putExtra(...)`, checking `intent!=null` is kind of useless. That's not the problem. I'd look at Activity.java line 2749 for the OS version you're using. That might provide a hint. You can find the framework sources [here](http://android.git.kernel.org/?p=platform/frameworks/base.git;a=summary) – Ted Hopp Mar 09 '11 at 04:22
  • Thank you Ted, however I don't see a problem there neither, or as far as I can tell. I also found out that, no matter what activity I want to start, it always throw me a null exception error. I tried with snippets for getting the contacts and dialing a number... – Aram Alvarez Mar 09 '11 at 06:14
  • I think problem may due to the method which is `protected`.Try making it `private` – Shashank_Itmaster Mar 09 '11 at 06:19
  • Thank you Shashank, I tried private and also even public but still no good. – Aram Alvarez Mar 09 '11 at 06:29

2 Answers2

0

The following article provides a workaround to this problem, not only for Samsung devices (I have an identical issue on HTC Desire Z running modded Android 2.3.7):

http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/

I realize it is quite awkward, but it works. To summarize, it suggests giving up attempts to get the original intent and just tells how to obtain a handle the captured photo.

ohaleck
  • 661
  • 4
  • 20
0

Try Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

I don't think the references of these constant strings are necessarily equal when their values are.

Matthew
  • 44,826
  • 10
  • 98
  • 87