单击图像后,相机应用程序崩溃
Posted
技术标签:
【中文标题】单击图像后,相机应用程序崩溃【英文标题】:camera app crashes after i click the image 【发布时间】:2017-05-17 05:18:40 【问题描述】:我正在创建一个 android,它在启动时会启动相机,当我单击图像时会启动一个弹出窗口,显示我单击的图像。弹出窗口有保存、重新加载和取消三个按钮。单击图像时应用程序崩溃。
public class MainActivity extends AppCompatActivity
private static final int CAMERA_REQUEST = 1888;
public static final String ALLOW_KEY = "ALLOWED";
public static final String CAMERA_PREF = "camera_pref";
Button Close;
Button reload;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openCamera();
private void openCamera() //i open the camera here
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CAMERA_REQUEST);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST)
initiatepopupwindow()
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
private PopupWindow pwindo;
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) this.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 OnClickListener()
public void onClick(View popupView)
pwindo.dismiss();
);
reload.setOnClickListener(new OnClickListener()
public void onClick(View popupView)
openCamera();
);
catch (Exception e)
e.printStackTrace();
日志
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 ResultInfowho=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_
android:layout_
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_
android:layout_ />
</LinearLayout>
弹窗布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@android:color/holo_blue_light"
android:id="@+id/popup">
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:background="@drawable/save"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<Button
android:id="@+id/button2"
android:layout_
android:layout_
android:background="@drawable/reload"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<Button
android:id="@+id/button3"
android:layout_
android:layout_
android:background="@drawable/error"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ImageView>
【问题讨论】:
请张贴logcat? 如果有人有替代方案,请发帖 我想你没有看到第一条评论?发布 logcat 我们将尝试帮助您自己解决问题 我发布了 logcat if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) 使用这个,请出示你的activity_main xml。 【参考方案1】:试试这个,
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
【讨论】:
@phoenixshiv 你没有正确更新你的代码它不是 this.findViewById(R.id.imageView1);它的 layout.findViewById(R.id.imageView1);检查我的答案一次 是的,我改变了它,它正在工作。非常感谢你的帮助 @phoenixshiv 很高兴!这有帮助:)【参考方案2】:问题在于弹出方法中您正在夸大其他布局并以错误的方式获取图像视图。
this.findViewById
将尝试从内容视图中获取图像视图,该内容视图设置为活动而不是来自弹出视图
所以从弹出视图中获取图像视图
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_
android:layout_
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/save"></Button>
<Button
android:id="@+id/button2"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/reload"></Button>
<Button
android:id="@+id/button3"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/error"></Button>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
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();
【讨论】:
你我的朋友真是天赐之物。它正在工作。非常感谢你的帮助以上是关于单击图像后,相机应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章