1

I am creating an android which when started launches the camera and when I click the image a popup is launched which shows the image I clicked. Popup has three buttons save,reload and cancel. App is crashing when I click the image.

  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();
        }
    }
}

Logs

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)

activity main

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

layout of popup

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

2 Answers2

0

Try this,

imageView1 is not in activity_main layout file its in popup.xml layout file

So its unable to initialize imageView and throwing NLE.

Move

imageView = (ImageView) this.findViewById(R.id.imageView1); 

from onCreate to

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


}
Raghavendra
  • 2,305
  • 25
  • 30
0

Issue is in popup method you are inflating other layout and taking imageview wrong way.

this.findViewById will try to get imageview from contentview which is set to activity not from popup view so take image view from 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>

Activity Code Change

@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();
        }
    }
user1140237
  • 5,015
  • 1
  • 28
  • 56