nullpointer的致命异常

时间:2018-02-01 17:55:02

标签: android android-studio button

当你点击btnChangePass时,我收到了错误。

我有错误:

  

致命的例外:主要                                                                           处理:com.company.learn2,PID:18854                                                                           显示java.lang.NullPointerException                                                                               在com.company.learn2.Heslo.changePassword(Heslo.java:84)                                                                               在com.company.learn2.Heslo.onClick(Heslo.java:69)                                                                               在android.view.View.performClick(View.java:4506)                                                                               在android.view.View $ PerformClick.run(View.java:18848)                                                                               在android.os.Handler.handleCallback(Handler.java:808)                                                                               在android.os.Handler.dispatchMessage(Handler.java:103)                                                                               在android.os.Looper.loop(Looper.java:193)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5341)                                                                               at java.lang.reflect.Method.invokeNative(Native Method)                                                                               在java.lang.reflect.Method.invoke(Method.java:515)                                                                               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:829)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)                                                                               在dalvik.system.NativeStart.main(本地方法)

我的活动:

public class Heslo extends AppCompatActivity implements View.OnClickListener {

    private EditText input_new_password;
    private Button btnChangePass;
    private Button btnLogout;


    private FirebaseAuth auth;
    private ImageButton imageButton;

    private RelativeLayout mIdLayout;

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

        input_new_password = (EditText)findViewById(R.id.dashboard_new_password);
        btnChangePass = (Button)findViewById(R.id.dashboard_btn_change_pass);
        imageButton = (ImageButton) findViewById(R.id.action_back);
        btnLogout = (Button) findViewById(R.id.dashboard_btn_logout);

        mIdLayout = (RelativeLayout) findViewById(R.id.idLayout);



        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Heslo.this, MainActivity.class);
                startActivity(intent);
            }
        });

        btnChangePass.setOnClickListener(this);
        btnLogout.setOnClickListener(this);

    }




    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.dashboard_btn_change_pass)
            changePassword(input_new_password.getText().toString());
        else if(view.getId() == R.id.dashboard_btn_logout)
            logoutUser();
    }

    private void logoutUser() {
        auth.signOut();
        if(auth.getCurrentUser() == null)
        {
            startActivity(new Intent(Heslo.this,Login.class));
            finish();
        }
    }

    private void changePassword(String newPassword) {
        FirebaseUser user = auth.getCurrentUser();
        user.updatePassword(newPassword).addOnCompleteListener(Heslo.this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if(task.isSuccessful())
                {
                    Snackbar snackBar = Snackbar.make(mIdLayout,"Password changed",Snackbar.LENGTH_SHORT);
                    snackBar.show();
                }

            }
        });
    }

}

我的xml:

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/idLayout">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/baf"
        android:background="@color/colorPrimary"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/action_back"
            android:background="@color/colorPrimary"
            android:layout_gravity="left"
            android:src="@drawable/ic_arrow_back_white"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Heslo"
            android:textSize="20dp"
            android:layout_marginStart="50dp"
            android:textColor="#FFFFFF"/>

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TextInputLayout
        android:layout_below="@+id/baf"
        android:id="@+id/dashboard_input_new_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <EditText
            android:id="@+id/dashboard_new_password"
            android:hint="Zadej nové heslo"
            android:inputType="textPassword"
            android:maxLines="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/dashboard_btn_change_pass"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/dashboard_input_new_password"
        android:background="#263238"
        android:text="Změnit heslo" />

    <Button
        android:id="@+id/dashboard_btn_logout"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/dashboard_btn_change_pass"
        android:background="#263238"
        android:text="Odhlásit se"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

At the line

user.updatePassword(newPassword).addOnCompleteListener(Heslo.this, new OnCompleteListener<Void>() {

you are getting a NullPointerException, this means some object there is null.

Check the user object, which you get from the line above: FirebaseUser user = auth.getCurrentUser();.

I guess Firebase is returning a null user...