Android - 弹出 window 崩溃
Android - Popup window crashing
在 Whosebug 社区中搜索了很多解决方案,但 none 解决了我的问题:我在 Main.java
activity 中有一个 button
,当 button
单击后,弹出菜单不显示,整个应用程序崩溃。
这是我的代码:
Button _buyurtmaYaratish = (Button) findViewById(R.id.btn_buyurtma_berish);
_buyurtmaYaratish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnPopup, 50, -30);
}
});
我的Main.xml代码:
<RelativeLayout.......
<Button
android:id="@+id/btn_buyurtma_berish"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/buyurtma_berish"
android:background="@drawable/button_kirish"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/white"
android:layout_below="@+id/edtx_summa_miqdori"/>
</RelativeLayout>
我的popup.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/txt_kk_mas_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/aloqaga_chiqamiz"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:textColor="@color/black"/>
<Button
android:layout_width="280dp"
android:layout_height="wrap_content"
android:background="@drawable/button_kirish"
android:text="@string/tasdiqlash"
android:textColor="@color/white"
android:textSize="19sp"
android:id="@+id/btn_popup"
android:layout_below="@+id/txt_kk_mas_popup"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
我做错了什么?
java.lang.NullPointerException
at datasite.com.aroba.DelHome.onClick(DelHome.java:49)
at android.view.View.performClick(View.java:4192)
at android.view.View$PerformClick.run(View.java:17248)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
at dalvik.system.NativeStart.main(Native Method)
当我尝试单击 button
时,它在 Logcat 中突出显示为红色
改变这个 :->
Button btnPopup = (Button) findViewById(R.id.btn_popup);
用这个 :-> Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
在你的代码中
喜欢下面的代码
Button _buyurtmaYaratish = (Button) findViewById(R.id.btn_buyurtma_berish);
_buyurtmaYaratish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnPopup, 50, -30);
}
});
我认为你在这里犯了错误:
Button btnPopup = (Button) findViewById(R.id.btn_popup);
改成这样:
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
使用 Button btnPopup = (Button)popupView.findViewById(R.id.btn_popup)
而不是 Button btnPopup = (Button) findViewById(R.id.btn_popup)
作为
使用这个:
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
而不是这个:
Button btnPopup = (Button) findViewById(R.id.btn_popup);
不要使用Popup window,您最好使用AlertDialog,它不仅易于构建,而且可以同时获得类似的背景效果。更灵活 move/change 并且工作顺畅
在 Whosebug 社区中搜索了很多解决方案,但 none 解决了我的问题:我在 Main.java
activity 中有一个 button
,当 button
单击后,弹出菜单不显示,整个应用程序崩溃。
这是我的代码:
Button _buyurtmaYaratish = (Button) findViewById(R.id.btn_buyurtma_berish);
_buyurtmaYaratish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnPopup, 50, -30);
}
});
我的Main.xml代码:
<RelativeLayout.......
<Button
android:id="@+id/btn_buyurtma_berish"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/buyurtma_berish"
android:background="@drawable/button_kirish"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/white"
android:layout_below="@+id/edtx_summa_miqdori"/>
</RelativeLayout>
我的popup.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/txt_kk_mas_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/aloqaga_chiqamiz"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:textColor="@color/black"/>
<Button
android:layout_width="280dp"
android:layout_height="wrap_content"
android:background="@drawable/button_kirish"
android:text="@string/tasdiqlash"
android:textColor="@color/white"
android:textSize="19sp"
android:id="@+id/btn_popup"
android:layout_below="@+id/txt_kk_mas_popup"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
我做错了什么?
java.lang.NullPointerException
at datasite.com.aroba.DelHome.onClick(DelHome.java:49)
at android.view.View.performClick(View.java:4192)
at android.view.View$PerformClick.run(View.java:17248)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
at dalvik.system.NativeStart.main(Native Method)
当我尝试单击 button
改变这个 :->
Button btnPopup = (Button) findViewById(R.id.btn_popup);
用这个 :-> Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
在你的代码中
喜欢下面的代码
Button _buyurtmaYaratish = (Button) findViewById(R.id.btn_buyurtma_berish);
_buyurtmaYaratish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnPopup, 50, -30);
}
});
我认为你在这里犯了错误:
Button btnPopup = (Button) findViewById(R.id.btn_popup);
改成这样:
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
使用 Button btnPopup = (Button)popupView.findViewById(R.id.btn_popup)
而不是 Button btnPopup = (Button) findViewById(R.id.btn_popup)
作为
使用这个:
Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup);
而不是这个:
Button btnPopup = (Button) findViewById(R.id.btn_popup);
不要使用Popup window,您最好使用AlertDialog,它不仅易于构建,而且可以同时获得类似的背景效果。更灵活 move/change 并且工作顺畅