1

i m trying to create popup with runnable but i m not able to. i always get "NULLPOINTEREXPEXTIONS" i m almost done with my android app, the only thing left is show popup after some time, i used runnable to show popup

here is my code, ERROR generate this line

View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element));

NOTE : popup_element is ID of popup layout xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >


 public void run() {
             //Do something after 100ms
              initiatePopupWindow();
           }

           private void initiatePopupWindow() {
           try {
           // We need to get the instance of the LayoutInflater
           LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element));
            pwindo = new PopupWindow(layout, 300, 370, true);
           pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

           Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
           btnClosePopup.setOnClickListener(cancel_button_click_listener);

           } catch (Exception e) {
           e.printStackTrace();
           }
        }

           private OnClickListener cancel_button_click_listener = new OnClickListener() {
           public void onClick(View v) {

        pwindo.dismiss();

           }
           };

         }, 700);
        }
     }

ERROR LOG

 D/gralloc_goldfish(906): Emulator without GPU emulation detected.
 W/System.err(906): java.lang.NullPointerException
 W/System.err(906):  at com.example.runnable_with_popup.MainActivity$1.initiatePopupWindow(MainActivity.java:43)
 W/System.err(906):  at com.example.runnable_with_popup.MainActivity$1.run(MainActivity.java:34)
 W/System.err(906):  at android.os.Handler.handleCallback(Handler.java:730)
 W/System.err(906):  at android.os.Handler.dispatchMessage(Handler.java:92)
 W/System.err(906):  at android.os.Looper.loop(Looper.java:137)
 W/System.err(906):  at android.app.ActivityThread.main(ActivityThread.java:5103)
 W/System.err(906):  at java.lang.reflect.Method.invokeNative(Native Method)
 W/System.err(906):  at java.lang.reflect.Method.invoke(Method.java:525)
 W/System.err(906):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 W/System.err(906):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 W/System.err(906):  at dalvik.system.NativeStart.main(Native Method)

Please help me out from this problem. i searched over internet but did not found example or question related to my problem help will be appreciated

thanks

Ashu Kumar
  • 832
  • 19
  • 38

2 Answers2

2

The parent might be missing, put false on the last part

View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element),false);
Sheychan
  • 2,415
  • 14
  • 32
  • i used **false ** but it does not work. also i m posting error log, hope you can find better solution @Sheychan – Ashu Kumar Aug 21 '15 at 01:36
  • Sorry, net was not working, i have to use this inside the acitvity ' LayoutInflater inflater = getLayoutInflater(); ' now popup is working with runnable , but popup is not showing on caller view :( @Sheychan – Ashu Kumar Aug 27 '15 at 08:35
0

use this one

 View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
Mahesh Gawhane
  • 348
  • 3
  • 20