我试图提示用户通过DialogFragment创建用户名。
当我显示dialogfragment时,我希望用户输入用户名,然后我将该用户名传回活动,然后写入Firebase:
DialogFragment:
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");
}
}
}
接收活动:
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)
答案 0 :(得分:1)
更改
Log.v("INPUT_TEXT", user.getDisplayName());
到
Log.v("INPUT_TEXT", "" + user.getDisplayName());
错误java.lang.NullPointerException: println needs a message
表示您的邮件必须在Log
!= null