2011-03-09 91 views
1

我有以下情況,這是相當拉我的頭髮:我正在寫一個類來管理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(); 
+0

既然你已經過了'intent.putExtra(...)',那麼檢查'intent!= null'是沒用的。這不是問題。我會看看你正在使用的操作系統版本的Activity.java 2749行。這可能會提供一個提示。你可以在這裏找到框架源碼(http://android.git.kernel.org/?p=platform/frameworks/base.git;a=summary) –

+0

謝謝泰德,但是我沒有看到問題沒有,或者據我所知。我還發現,無論我想開始什麼活動,它總是會拋出一個空的異常錯誤。我試着用snippets來獲取聯繫人並撥打一個號碼... –

+0

我認爲問題可能是由於受保護的方法所致。請嘗試使用私人的方式 –

回答

0

嘗試Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

我不認爲這些常量字符串的引用時,他們的價值必然相等。

+0

謝謝馬修,我嘗試過這一行,但仍然沒有任何...我遇到了同樣的問題。 –