Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: startActivityForResult always null and force close my app
    primarykey
    data
    text
    <p>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:</p> <pre><code>startActivityForResult(intent, 0); </code></pre> <p>However I get System.out.println("intent not null"); printed ok in LogCat...</p> <p>The LogCat says:</p> <pre><code>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) </code></pre> <p>In the manifest I have:</p> <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.CAMERA" /&gt; &lt;uses-feature android:name="android.hardware.camera"/&gt; &lt;activity android:name=".cameratest" /&gt; </code></pre> <p><strong>So, what I'm doing wrong?? Why startActivityForResult always throws null!?</strong></p> <pre><code>@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);** &lt;== null pointer exception } </code></pre> <p><strong>EDITED:</strong></p> <p>As per suggestion, I took a look to line 2749 on Activity.java from Android 2.1r2 framework and here is the code:</p> <pre><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 &gt;= 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); } } </code></pre> <p>Line 2749 is marked with double ** However, I don't see a problem with that code </p> <p><strong>EDITED</strong></p> <p>Forgot to explain how I call this:</p> <pre><code>cameratest cam = new cameratest(); cam.startCameraActivity(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload