使用Runnable Thread Android创建弹出窗口

时间:2015-08-20 01:39:43

标签: java android android-popupwindow

我试图用runnable创建弹出窗口,但我无法做到。我总是得到" NULLPOINTEREXPEXTIONS" 我几乎完成了我的Android应用程序,剩下的唯一一件事是show popup一段时间后,我用runnable来显示弹出窗口

这是我的代码,ERROR生成此行

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

注意:popup_element是弹出布局xml文件的ID

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

错误日志

&#13;
&#13;
 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)
&#13;
&#13;
&#13;

请帮我解决这个问题。我通过互联网搜索,但没有找到与我的问题相关的示例或问题 将不胜感激

谢谢

2 个答案:

答案 0 :(得分:2)

父母可能遗失了,在最后一部分放错了

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

答案 1 :(得分:0)

使用这个

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