【发布时间】:2014-07-25 03:06:00
【问题描述】:
我正在使用以下代码 sn-p 调用相机:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
getActivity().startActivityForResult(intent, INSTANCE_CAMERA);
onActivity 结果我的代码 sn-p 是:
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == active.RESULT_OK) {
if (requestCode == INSTANCE_CAMERA) {
// Log.d(this.getTag(), noMediaFile.getAbsolutePath());
// Bitmap bitmImage = BitmapFactory.decodeFile(noMediaFile.getAbsolutePath());
Bitmap bitmImage = null;
bitmImage = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream captureCardImage = new ByteArrayOutputStream();
bitmImage.compress(Bitmap.CompressFormat.PNG, 100,
captureCardImage);
byteValue = captureCardImage.toByteArray();
String imageConvertString = Base64.encodeToString(byteValue,
Base64.DEFAULT);
// Issue is this the imageView
ImageView imvPicture = (ImageView) active.findViewById(R.id.imvPicture);
Log.d(TAG, bitmImage + " My Fragment Tag");
imvPicture.setImageBitmap(bitmImage);
}
}
}
但是 ImageView 给了我空指针
我的 XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>
<ImageView
android:id="@+id/imvPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/picture_snapshot" />
<Button
android:id="@+id/btnNewEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/shady_yellow"
android:text="@string/title"
android:textStyle="bold"
android:textColor="@android:color/black" />
</FrameLayout>
我在 onActivityResult 的 ImageView 上得到空指针异常
记录猫错误:
原因:
java.lang.NullPointerException
at com.***.*****.com.**.onActivityResult(TakeNoteFragment.java:150)
at com.texticky.app.activities.MainActivity.onActivityResult(MainActivity.java:65)
at android.app.Activity.dispatchActivityResult(Activity.java:5390)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
at android.app.ActivityThread.access$1200(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
【问题讨论】:
-
active中的ImageView imvPicture = (ImageView) active.findViewById(R.id.imvPicture);是什么? -
活动:getActivity()
-
有时会发生的错误:你是否在 onCreate() 中引用了相同的布局?
-
您正在从片段中调用意图,因此您的 startactivity for result 将调用您的活动 onactivity result 而不是您的片段
标签: android android-fragments android-activity android-camera android-imageview