This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
在11个月前关闭。
我想使用
场景是用户单击
我已经成功显示了
我不知道如何在何处调用
这是我的代码ProfileActivity.java,底部带有
这是我的ChangePassDialog.java
还有Dialog.xml
原木猫
感谢任何帮助!
(12个答案)
在11个月前关闭。
我想使用
ProfileActivity.java
库从弹出的Retrofit
更改Dialog
中的用户密码。场景是用户单击
Dialog
中的Edit Button
后将显示MainActivity.java
。我已经成功显示了
Dialog
并将if-else放在上面,但是问题是我无法将数据发送到Retrofit
中的ProfileActivity.java
方法。我不知道如何在何处调用
Retrofit
方法,这会使应用程序崩溃。这是我的代码ProfileActivity.java,底部带有
Retrofit
方法:public class ProfileActivity extends AppCompatActivity implements ChangePassDialog.ChangePassDialogListener {
private String oldPassExtra, newPassExtra;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Button btnEditPass = findViewById(R.id.btn_editPass);
btnEditPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChangePass changePassDialog = new ChangePass();
changePassDialog.show(getSupportFragmentManager(),"Pass Dialog Changer");
}
});
}
public void changePassApi(String oldPass, String newPass) {
RestApi api = RetroFit.getInstanceRetrofit();
Call<ResponseRegister> registerCall = api.changePassword(
oldPass,
newPass
);
registerCall.enqueue(new Callback<ResponseRegister>() {
// If response success
@Override
public void onResponse(Call<ResponseRegister> call, Response<ResponseRegister> response) {
if (response.isSuccessful()) {
String result = response.body().getResult();
String msg = response.body().getMsg();
if (result.equals("1")) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Connection Failed!", Toast.LENGTH_LONG).show();
}
}
// if response fail
@Override
public void onFailure(Call<ResponseRegister> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Connection Problem", Toast.LENGTH_LONG).show();
}
});
}
}
这是我的ChangePassDialog.java
public class ChangePass extends DialogFragment {
ProfileActivity profileActivity;
private EditText edtOldPass, edtNewPass;
private TextView okPass, cancelPass;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_changepass, container, false);
okPass = view.findViewById(R.id.ok_pass);
cancelPass = view.findViewById(R.id.cancel_pass);
edtOldPass = view.findViewById(R.id.edit_passOld);
edtNewPass = view.findViewById(R.id.edit_passNew);
profileActivity = new ProfileActivity();
cancelPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDialog().dismiss();
}
});
okPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inputOld = edtOldPass.getText().toString().trim();
String inputNew = edtNewPass.getText().toString().trim();
if (inputOld.isEmpty()) {
edtOldPass.requestFocus();
edtOldPass.setError("Field password lama tidak boleh kosong");
// Toast.makeText(getActivity(),"hallo",Toast.LENGTH_SHORT).show();
} else if (inputNew.isEmpty()) {
edtNewPass.requestFocus();
edtNewPass.setError("Field password baru tidak boleh kosong");
} else if (inputOld.equals(inputNew)) {
edtNewPass.requestFocus();
edtNewPass.setError("Password tidak boleh sama");
} else {
profileActivity.changePassApi(inputOld, inputNew);
}
}
});
return view;
}
}
还有Dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_passOld"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="10dp"
android:hint="Password lama"
android:inputType="textPassword"
android:paddingStart="25dp"
android:paddingLeft="25dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
<EditText
android:id="@+id/edit_passNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit_passOld"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:hint="Password baru"
android:inputType="textPassword"
android:paddingStart="25dp"
android:paddingLeft="25dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
<TextView
android:id="@+id/ok_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_passNew"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginRight="25dp"
android:text="OK"
android:textColor="@color/purple"
android:textSize="16sp" />
<TextView
android:id="@+id/cancel_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_passNew"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:text="CANCEL"
android:textColor="@color/purple"
android:textSize="16sp" />
</RelativeLayout>
原木猫
01-25 14:40:15.630 2102-2102/com.supermalkarawaci.redeempointapps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.supermalkarawaci.redeempointapps, PID: 2102
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.supermalkarawaci.redeempointapps.activity.ProfileActivity$4.onResponse(ProfileActivity.java:340)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5480)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
感谢任何帮助!
最佳答案
profileActivity =新的ProfileActivity();
您无法获得像这样的ProfileActivity对象,而且永远也不会这样做。它将初始化ProfileActivity的全新实例,该实例与您的运行活动无关。
像这样做。
profileActivity =(ProfileActivity)getActivity();
选项2。
喜欢这个
将public void changePassApi()
方法放在Dialog片段中
09-10 06:23