• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Android:startActivityForResult始终为空并强制关闭我的应用程序

Android 来源:Aram Alvarez 7次浏览

我有以下情况,这是相当拉我的头发:我正在写一个类来管理Android相机,它扩展了活动。在那个类里面我有下面的代码。什么情况是,我总是得到零点异常–with其强制关闭小friend–在这行:Android:startActivityForResult始终为空并强制关闭我的应用程序

startActivityForResult(intent, 0); 

但是我得到的System.out.println(“故意不为空”);印在logcat中确定…

的logcat的说:

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) 

在清单中,我有:

<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" /> 

所以,我在做什么错?为什么startActivityForResult总是抛出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 
} 

编辑:

按照建议,我接过来一看从Android 2.1r2框架上Activity.java线2749和这里是代码:

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

线2749标有双**但是,我没有看到该代码的问题

EDITED

忘了解释我是如何把这种:

cameratest cam = new cameratest(); 
cam.startCameraActivity(); 


===========解决方案如下:

尝试Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

我不认为这些常量字符串的引用时,他们的价值必然相等。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)