我正在尝试创建一个弹出窗口,该窗口具有一个EditText字段供用户输入新预算和一个用于保存值的按钮。单击“保存”按钮后,主活动中的TextView应该根据用户在弹出窗口中的预算(EditText)中输入的内容进行更改。但是,当我为popupwindow中的“保存”按钮实现OnClickListener时,一旦我尝试从主活动导航到popupwindow,应用程序就会崩溃。
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.spending_budgetpopup, null);
// create the popup window
int width = RelativeLayout.LayoutParams.WRAP_CONTENT;
int height = RelativeLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
Button button_savebudget = findViewById(R.id.budget_save);
button_savebudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String budget = newbudget.getText().toString();
amount_budget.setText(budget);
}
});
}
上面的代码显示了OnClick方法,该方法从主要活动中打开PopupWindow。而“ button_savebudget”将保存该值并在主活动中更改TextView。
logcat在下面:-
--------- beginning of crash
10-11 16:08:25.552 4980-4980/com.example.hannzern1998.spendingtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hannzern1998.spendingtest, PID: 4980
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.hannzern1998.spendingtest.MainActivity.onButtonShowPopupWindowClick(MainActivity.java:100)
at com.example.hannzern1998.spendingtest.MainActivity$1.onClick(MainActivity.java:44)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我很确定“ button_savebudget”是导致崩溃的原因,因为一旦删除了特定的代码段,应用程序就不会崩溃,但是我不确定还有什么要解决的问题!
答案 0 :(得分:1)
尝试
var poolData = {
UserPoolId : '...', // your user pool id here
ClientId : '...' // your app client id here
Secretkey: '...'// your app secret key here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);