gpt4 book ai didi

java - 将数据从 DialogFragment 传递到 Activity 时出现 NullPointer

转载 作者:行者123 更新时间:2023-12-02 12:05:17 25 4
gpt4 key购买 nike

我试图提示用户通过 DialogFragment 创建用户名。

当我显示对话框 fragment 时,我希望用户输入用户名,然后将该用户名传递回 Activity ,然后写入 Firebase:

对话框 fragment :

public class DisplayNameDialogFragment extends DialogFragment {

//TODO: Is this going to cause compile-time exception in these versions?
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

LayoutInflater inflater = getActivity().getLayoutInflater();
View mView = inflater.inflate(R.layout.fragment_display_name_dialog, null);
final EditText displayName = (EditText) mView.findViewById(R.id.display_name);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.DISPLAY_NAME_LABEL);
builder.setView(mView);
builder.setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String displayNameEntered = displayName.getText().toString();
listener.onFinishEditDialog(displayNameEntered);
}
});

return builder.create();
}

public static interface EditNameDialogListener {
public abstract void onFinishEditDialog(String inputText);
}

private EditNameDialogListener listener;

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
this.listener = (EditNameDialogListener)getActivity();
} catch (final ClassCastException e){
throw new ClassCastException(getActivity().toString() + "must implement listener");
}
}
}

接收 Activity :

public class SignupActivity extends AppCompatActivity implements DisplayNameDialogFragment.EditNameDialogListener{
private static final String TAG = "SignupActivity";

@Bind(R.id.input_email)
EditText _emailText;
@Bind(R.id.input_password)
EditText _passwordText;
@Bind(R.id.btn_signup)...........

@Override
public void onFinishEditDialog(String inputText) {
FirebaseAuth auth = FirebaseAuth.getInstance();
UserProfileChangeRequest m = new UserProfileChangeRequest.Builder()
.setDisplayName(inputText).build();
FirebaseUser user = auth.getCurrentUser();
user.updateProfile(m);
Log.v("INPUT_TEXT", user.getDisplayName());
Toast.makeText(getApplicationContext(), inputText, Toast.LENGTH_LONG).show();
Intent toHome = new Intent(SignupActivity.this, HomeActivity.class);
startActivity(toHome);
}
}

我收到一个 NullPointer,但不确定原因:

java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.v(Log.java:118)
at com.troychuinard.fanpolls.SignupActivity.onFinishEditDialog(SignupActivity.java:367)
at com.troychuinard.fanpolls.Fragment.DisplayNameDialogFragment$1.onClick(DisplayNameDialogFragment.java:39)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

最佳答案

改变

Log.v("INPUT_TEXT", user.getDisplayName());

Log.v("INPUT_TEXT", "" + user.getDisplayName());

错误java.lang.NullPointerException:println需要一条消息意味着您在Log中的消息必须!= null

关于java - 将数据从 DialogFragment 传递到 Activity 时出现 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com