Android:startActivityForResult始终为空并强制关闭我的应用程序
我有以下情况,这是相当拉我的头发:我正在写一个类来管理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);
我不认为这些常量字符串的引用时,他们的价值必然相等。
回答:
下面的文章提供了解决这个问题,不仅为三星设备(我对HTC Desire Z的相同的问题,运行Android的改装2.3.7):
http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
我意识到这是相当尴尬,但它的作品。总而言之,它建议放弃尝试获得原始意图,并只告诉如何获取拍摄照片的句柄。
以上是 Android:startActivityForResult始终为空并强制关闭我的应用程序 的全部内容, 来源链接: utcz.com/qa/262132.html