如何让Android中的popupWindow正常工作?

时间:2012-09-11 22:30:32

标签: java android logging popup

我试图让一个简单的弹出窗口发生,但是返回了一个错误。错误发生在提供的代码的第四行(最后一个)。有什么建议吗?

活动代码

logoutoption = new PopupWindow();
logoutoption.showAtLocation(new LinearLayout(this), Gravity.CENTER, 0, 0);
logoutoption.setBackgroundDrawable(getResources().getDrawable(drawable.logoutoption_default));
logoutoption.update(100, 50);

LOG

09-11 22:26:13.324: E/AndroidRuntime(1232): FATAL EXCEPTION: main
09-11 22:26:13.324: E/AndroidRuntime(1232): java.lang.NullPointerException
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.widget.PopupWindow.update(PopupWindow.java:1322)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at My.Taste.App.MyTasteActivity.getLogoutOption(MyTasteActivity.java:487)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at My.Taste.App.MyTasteActivity.access$0(MyTasteActivity.java:478)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at My.Taste.App.MyTasteActivity$3.onClick(MyTasteActivity.java:313)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.view.View.performClick(View.java:3511)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.view.View$PerformClick.run(View.java:14105)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.os.Handler.handleCallback(Handler.java:605)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.os.Looper.loop(Looper.java:137)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 22:26:13.324: E/AndroidRuntime(1232):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

替换为以下代码:

LayoutInflater layoutInflater  = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popupView = layoutInflater.inflate(R.layout.logoutoption, null);  
        final PopupWindow logoutoption = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
        logoutoption.showAsDropDown(search, 50, -30);