我正在创建一个android,当它启动时启动相机,当我点击图像时会弹出一个弹出窗口,显示我点击的图像。 Popup有三个按钮保存,重新加载和取消。单击图像时应用程序崩溃。
01-03 13:25:23.061 22093-22093/com.example.shivadeeps.aapam_auto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shivadeeps.aapam_auto, PID: 22093
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/165 flg=0x1 (has extras) }} to activity {com.example.shivadeeps.aapam_auto/com.example.shivadeeps.aapam_auto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4756)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4799)
at android.app.ActivityThread.access$1500(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.example.shivadeeps.aapam_auto.MainActivity.onActivityResult(MainActivity.java:70)
at android.app.Activity.dispatchActivityResult(Activity.java:6833)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4752)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4799)
at android.app.ActivityThread.access$1500(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
记录
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shivadeeps.aapam_auto.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
活动主要
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:id="@+id/popup">
<Button
android:id="@+id/button1"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:background="@drawable/save"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<Button
android:id="@+id/button2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:background="@drawable/reload"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<Button
android:id="@+id/button3"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:background="@drawable/error"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ImageView>
弹出窗口布局
User
答案 0 :(得分:0)
试试这个,
imageView1 不在activity_main 布局文件在popup.xml中布局文件
因此无法初始化imageView并抛出NLE。
移动
imageView = (ImageView) this.findViewById(R.id.imageView1);
从onCreate到
private void initiatePopupWindow() {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.layout,
(ViewGroup) findViewById(R.id.popup));
imageView = (ImageView) layout.findViewById(R.id.imageView1); //add this line
...//your code
}
答案 1 :(得分:0)
问题在弹出窗口方法中,你正在给其他布局充气并采取错误的方式。
this.findViewById
将尝试从contentview获取imageview,该视图设置为活动而非弹出视图
所以从popupview中获取图像视图
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/save"></Button>
<Button
android:id="@+id/button2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/reload"></Button>
<Button
android:id="@+id/button3"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/error"></Button>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_delete"></ImageView>
</LinearLayout>
活动代码更改
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
initiatePopupWindow();
Bitmap bmp = (Bitmap) data.getExtras().get("data");
if (imageView != null) {
imageView.setImageBitmap(bmp);
} else {
Log.e("ImageView", "Popup imageview NULL");
}
}
private PopupWindow pwindo;
private ImageView imageView;
private void initiatePopupWindow() {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.test, null);
imageView = (ImageView) layout.findViewById(R.id.imageView1);
android.os.Handler handler = new android.os.Handler();
try {
handler.postDelayed(new Runnable() {
public void run() {
pwindo = new PopupWindow(layout, 700, 770, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}, 100);
Button close = (Button) layout.findViewById(R.id.button3);
Button reload = (Button) layout.findViewById(R.id.button2);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View popupView) {
pwindo.dismiss();
}
});
reload.setOnClickListener(new View.OnClickListener() {
public void onClick(View popupView) {
openCamera();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}