snackbar在popupwindow中无法正常工作。这是我试过的代码。 首先我创建了popupwindow
//popupwindow
LayoutInflater inflater = (LayoutInflater) AddMembersToEventActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View editFamilyPopupView = inflater.inflate(R.layout.activity_edit_family_member_popup,null);
final RelativeLayout parentLayout = (RelativeLayout)editFamilyPopupView.findViewById(R.id.parent_layout);
final PopupWindow editFamilyPopupWindow = new PopupWindow(
editFamilyPopupView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
....
调用snakcbar方法
showSnackBar(getString(R.string.rashi_msg),parentLayout);
...
显示小吃栏
//show snackbar
public void showSnackBar(String message,ViewGroup viewGroup){
Snackbar snackbar = Snackbar
.make(viewGroup, message, Snackbar.LENGTH_LONG);
snackbar.show();
}
//...
//exception
02-20 13:52:20.747 20762-20762/divine.calcify.com.divine E/AndroidRuntime: FATAL EXCEPTION: main
Process:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.support.design.widget.Snackbar.<init>(Snackbar.java:188)
at android.support.design.widget.Snackbar.make(Snackbar.java:220)
at divine.calcify.activities.AddMembersToEventActivity.showSnackBar(AddMembersToEventActivity.java:866)
at divine.calcify.activities.AddMembersToEventActivity$9.onClick(AddMembersToEventActivity.java:663)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
如何让小吃栏出现在弹出窗口上方?
答案 0 :(得分:0)
您收到该异常是因为Snackbar无法附加到view
。
更详细地说明实际发生的事情是Snackbar
在viewGroup
上调用getContext(),这似乎是null
并导致异常被引发。
P.S。请注意,链接指向View.getContext()
,但可以ViewGroup
继承。{/ p>