安卓:startActivityForResult总是空,并强制关闭我的应用程序 [英] Android: startActivityForResult always null and force close my app
问题描述
我有以下情形颇为拉我的头发了:我正在写一个类来管理Android摄像头,它扩展了活动。在那个班我下面的code。什么情况是,我总是得到零点异常--with其强制关闭小friend--在该行,上面写着:
startActivityForResult(意向,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):在android.app.Activity.startActivityForResult(Activity.java:2749)
03-08 22:46:38.584:ERROR / AndroidRuntime(1079):在com.test.cameratest.startCameraActivity(cameratest.java:39)
在清单中,我有:
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.CAMERA/>
<使用特征的android:NAME =android.hardware.camera/>
<活动机器人:cameratestNAME = />
所以,我在做什么错了?为什么startActivityForResult总是抛出空!?
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
}
/ **根据建议,试图改变方法,从受保护的私人或公共
但仍然有同样的问题* /
保护无效startCameraActivity()
{
字符串路径=(新StringBuilder()).append(Environment.getExternalStorageDirectory()).append("/images/test.jpg").toString();
的System.out.println(开始);
档案文件=新的文件(路径);
的System.out.println(路径);
乌里outputFileUri = Uri.fromFile(文件);
意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
intent.putExtra(输出,outputFileUri);
/ **删除按照建议
如果(意向!= NULL)
{
的System.out.println(故意不为空);
** startActivityForResult(意向,0); **
}
* /
** startActivityForResult(意向,0); **< ==空指针异常
}
编辑:
根据建议,我接过来一看行2749对Activity.java从Android的2.1r2框架,这里是code:
公共无效startActivityForResult(意向意图,诠释请求code){
如果(mParent == NULL){
****线2749是如下因素
Instrumentation.ActivityResult AR =
mInstrumentation.execStartActivity(
对此,mMainThread.getApplicationThread(),mToken,这一点,
意图,请求code);
****
如果(AR!= NULL){
mMainThread.sendActivityResult(
mToken,mEmbeddedID,请求code,ar.getResult code()
ar.getResultData());
}
如果(要求code取代; = 0){
//如果这个开始请求的结果,我们能够避免
//活性可见,直到结果被接收。环境
//的onCreate(捆绑savedInstanceState)或onResume(在此code)将保持
//活动在这段时间隐藏的,以避免闪烁。
//当结果被请求,因为这只能进行
//,保证我们获取信息回来的时候
//活动完成后,不管发生了什么。
mStartedActivity =真;
}
} 其他 {
mParent.startActivityFromChild(这一点,意图,请求code);
}
}
线2749标有双**不过,我看不出与code问题
EDITED
忘了解释我是怎么称呼这样的:
cameratest凸轮=新cameratest();
cam.startCameraActivity();
下面的文章提供了一个解决此问题的,不仅为三星设备(我对HTC Desire Z的一个相同的问题,运行Android的改装2.3.7):
<一个href="http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/" rel="nofollow">http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
我知道这是很尴尬的,但它的作品。总之,它表明放弃尝试获取原意,只是告诉您如何获取句柄拍摄的照片。
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:
startActivityForResult(intent, 0);
However I get System.out.println("intent not null"); printed ok in LogCat...
The LogCat says:
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)
In the manifest I have:
<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" />
So, what I'm doing wrong?? Why startActivityForResult always throws 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
}
EDITED:
As per suggestion, I took a look to line 2749 on Activity.java from Android 2.1r2 framework and here is the 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 >= 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);
}
}
Line 2749 is marked with double ** However, I don't see a problem with that code
EDITED
Forgot to explain how I call this:
cameratest cam = new cameratest();
cam.startCameraActivity();
The following article provides a workaround to this problem, not only for Samsung devices (I have an identical issue on HTC Desire Z running modded Android 2.3.7):
http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
I realize it is quite awkward, but it works. To summarize, it suggests giving up attempts to get the original intent and just tells how to obtain a handle the captured photo.
这篇关于安卓:startActivityForResult总是空,并强制关闭我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!