我有以下情况,这完全拉我的头发:我正在写一个类来管理Android相机,它扩展了Activity。在该课程内,我有以下代码。会发生什么事情,我总是得到零点异常 - 它的力量接近小朋友 - 在行中说:
startActivityForResult(intent, 0);
但是我得到System.out.println(“intent not null”);在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总是抛出空??
@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 :(得分:0)
尝试Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
我认为这些常量字符串的引用在它们的值为时必然是相等的。
答案 1 :(得分:0)
以下文章提供了此问题的解决方法,不仅适用于三星设备(我在HTC Desire Z运行模块化Android 2.3.7上有相同的问题):
http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
我意识到这很尴尬,但它确实有效。总而言之,它建议放弃尝试获取原始意图,并告诉如何获取处理捕获的照片。