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();