NullPointer的问题

时间:2018-01-20 18:47:39

标签: android firebase android-intent firebase-realtime-database firebaseui

我需要转到第二个活动,但是当我点击按钮时,它会写信给我:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.application / com.example.application.Firebase}:java.lang.NullPointerException                                                                              在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)                                                                              在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)                                                                              在android.app.ActivityThread.access $ 800(ActivityThread.java:152)                                                                              在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1343)                                                                              在android.os.Handler.dispatchMessage(Handler.java:110)                                                                              在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)                                                                              at dalvik.system.NativeStart.main(Native Method)                                                                           引起:java.lang.NullPointerException                                                                            一个                                                                              在android.app.Activity.performCreate(Activity.java:5350)                                                                              在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)

这是Firebase活动:

public class Firebase extends AppCompatActivity {


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

        mlistView = (ListView) findViewById(R.id.listView);

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://example.firebase/Users");

        FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
                this,
                String.class,
                android.R.layout.simple_list_item_1,
                databaseReference
        ) {
            @Override
            protected void populateView(View v, String model, int position) {

                TextView textView = (TextView) v.findViewById(android.R.id.text1);
                textView.setText(model);




            }
        };
        mlistView.setAdapter(firebaseListAdapter);
    }
}

这是第一项活动:

public class DashBoard extends AppCompatActivity implements View.OnClickListener {

    private TextView txtWelcome;
    private EditText input_new_password;
    private Button btnChangePass,btnLogout;
    private RelativeLayout activity_dashboard;

    private FirebaseAuth auth;

    private Button mbtnIntent;

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

        //View
        txtWelcome = (TextView)findViewById(R.id.dashboard_welcome);
        input_new_password = (EditText)findViewById(R.id.dashboard_new_password);
        btnChangePass = (Button)findViewById(R.id.dashboard_btn_change_pass);
        btnLogout = (Button)findViewById(R.id.dashboard_btn_logout);
        activity_dashboard = (RelativeLayout)findViewById(R.id.activity_dash_board);

        mbtnIntent = (Button) findViewById(R.id.btnIntent);

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

        //Init Firebase
        auth = FirebaseAuth.getInstance();

        //Session check
        if(auth.getCurrentUser() != null)
            txtWelcome.setText("Welcome , "+auth.getCurrentUser().getEmail());

        mbtnIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DashBoard.this), Firebase.class);
                startActivity(intent);
            }
        });




    }

    @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(DashBoard.this,MainActivity.class));
            finish();
        }
    }

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

这是manifest.xml:`

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DashBoard" />
    <activity android:name=".ForgotPassword" />
    <activity android:name= ".Firebase"/>
    <activity android:name=".SignUp"></activity>
</application>

`

我应该写什么?

2 个答案:

答案 0 :(得分:0)

将您的代码更改为:

Intent intent = new Intent(DashBoard.this,Firebase.class);
startActivity(intent);

并检查你是否在Android Manifest.Xml中开始了第二个活动:

<activity android:name=“.Firebase” />

答案 1 :(得分:0)

似乎listViewtext1中的一个或两个不在activity_main.xml类的布局Firebase内。确保它们在那里并且没有拼写错误。