创建个人资料活动[重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

你好我们现在正在尝试创建一个用户配置文件几周,我想添加一个按钮和文本输入布局,以便用户可以编辑那里的配置文件,但每次我点击保存按钮应用程序崩溃我使用简化编码的方法

public class ProfileActivity extends AppCompatActivity {

private static final int CHOOSE_IMAGE = 100;

ImageView buttonmessages;
ImageView selectimage;
ImageView share;
ImageView backgroundd;
ImageView notverified;
Button savebt;


private TextInputEditText profilename;
TextInputEditText email;
TextInputEditText phone;
TextInputEditText password;

Uri uriProfileImage;

String profileImageUrl;

FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    buttonmessages = findViewById(R.id.btmessages);
    backgroundd  = findViewById(R.id.upload2);
    share = findViewById(R.id.share);
    selectimage = findViewById(R.id.upload);
    notverified = findViewById(R.id.notverified);
    profilename= findViewById(R.id.um);
    email  =findViewById(R.id.proemail);
    phone = findViewById(R.id.prophone);
    password = findViewById(R.id.Propassword);
    savebt =findViewById(R.id.savebutton);



    buttonmessages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(ProfileActivity.this,
                    ChatActivity.class);
            startActivity(myIntent);
        }
    });
    selectimage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showImageChooser();
        }
    });

    backgroundd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showImageChooser();
        }
    });

    findViewById(R.id.savebutton).setOnClickListener(new 
  View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveUserInformation();
        }
    });


}
private void saveUserInformation() {


    String displayName = profilename.getText().toString();

    if (displayName.isEmpty()) {
        profilename.setError("Name required");
        profilename.requestFocus();
        return;
    }

    FirebaseUser user = mAuth.getCurrentUser();

    if (user != null && profileImageUrl != null) {
        UserProfileChangeRequest profile = new 
  UserProfileChangeRequest.Builder()
                .setDisplayName(displayName)
                .setPhotoUri(Uri.parse(profileImageUrl))
                .build();

        user.updateProfile(profile)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(ProfileActivity.this, "Profile 
Updated", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

这是我的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.ayodkay.app.bookshare, PID: 18097
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.support.design.widget.TextInputEditText.getText()' on a null object reference
                  at com.ayodkay.app.bookshare.ProfileActivity.saveUserInformation(ProfileActivity.java:104)
                  at com.ayodkay.app.bookshare.ProfileActivity.access$100(ProfileActivity.java:29)
                  at com.ayodkay.app.bookshare.ProfileActivity$4.onClick(ProfileActivity.java:95)
                  at android.view.View.performClick(View.java:5052)
                  at android.view.View$PerformClick.run(View.java:20162)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5753)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

如果你知道我可以做任何事情来解决这个问题,我将非常高兴,或者如果你有另一种方法,我也会很感激

android nullpointerexception
1个回答
0
投票

profilename为null,检查您是否正在膨胀此视图,可能您正在引用一个ID,它不在您正在膨胀的布局中

© www.soinside.com 2019 - 2024. All rights reserved.