Ashu Kumar
Ashu Kumar

Reputation: 832

Create Popup with Runnable Thread Android

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

Upvotes: 1

Views: 554

Answers (2)

Mahesh Gawhane
Mahesh Gawhane

Reputation: 338

use this one

 View popupView = layoutInflater.inflate(R.layout.popup_layout, null);

Upvotes: 0

Sheychan
Sheychan

Reputation: 2436

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

Upvotes: 2

Related Questions