2017-01-03 110 views
1

我创建了一个android,当它开始启动相机,当我点击图片时弹出一个弹出窗口,显示我点击的图片。 Popup有三个按钮保存,重新加载和取消。点击图片时,应用程序崩溃。我点击图片后,相机应用程序崩溃

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 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> 

+1

请张贴logcat的? – Raghavendra

+0

如果有人有替代方案请发帖 – phoenixshiv

+0

我想你没有看到第一个评论?发布logcat我们将尝试帮助你自己修复这个问题 – Raghavendra

回答

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 


} 
+0

没有工作bro – phoenixshiv

+0

仍崩溃与同样的错误? – Raghavendra

+0

是的,它再次崩溃,同样的错误 – phoenixshiv

0

的问题是,在弹出的方法你正在膨胀等的布局,并采取ImageView的错误的方式。

this.findViewById将尝试从内容查看的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(); 
     } 
    } 
+1

你我的朋友是天赐良机。它正在工作。非常感谢你的帮助 – phoenixshiv

+0

高兴地帮助..干杯:) – user1140237