如何在 Android Studio 中的 PopUp Window 中使用 setText() 方法?
How to use the setText() Method in a PopUp Window In Android Studio?
我的 activity 中有一个 editText
框,用户可以在其中输入 his/her phone 号码,然后单击 Next
按钮。现在,在单击按钮时,我膨胀了一个弹出视图,显示用户 his/her 输入的号码并要求确认。
这里的问题是我无法从 EditText
访问数字以在弹出视图中显示它。谁能告诉我怎么做?
布局充气器:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.confirm_number_popup, null);
ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView();
Button btn1 = popupView.findViewById(R.id.btn_edit);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width,
height, false);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
当我在充气器中添加这个时,我得到一个空指针异常:
TextView txt_phone_confirm =
findViewById(R.id.txt_phone_number_confirm);
EditText txt_Phone = findViewById(R.id.Phone_Number);
txt_phone_confirm.setText(txt_Phone.getText().toString());
这样做,我得到:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
在 popupView
上调用按 id 方法查找视图,因为这是具有您的 TextView
:
的视图
TextView txt_phone_confirm = popupView.findViewById(R.id.txt_phone_number_confirm);
这将为您提供刚刚扩充的文本视图。
我的 activity 中有一个 editText
框,用户可以在其中输入 his/her phone 号码,然后单击 Next
按钮。现在,在单击按钮时,我膨胀了一个弹出视图,显示用户 his/her 输入的号码并要求确认。
这里的问题是我无法从 EditText
访问数字以在弹出视图中显示它。谁能告诉我怎么做?
布局充气器:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.confirm_number_popup, null);
ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView();
Button btn1 = popupView.findViewById(R.id.btn_edit);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width,
height, false);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
当我在充气器中添加这个时,我得到一个空指针异常:
TextView txt_phone_confirm =
findViewById(R.id.txt_phone_number_confirm);
EditText txt_Phone = findViewById(R.id.Phone_Number);
txt_phone_confirm.setText(txt_Phone.getText().toString());
这样做,我得到:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
在 popupView
上调用按 id 方法查找视图,因为这是具有您的 TextView
:
TextView txt_phone_confirm = popupView.findViewById(R.id.txt_phone_number_confirm);
这将为您提供刚刚扩充的文本视图。