如何识别此错误的来源“java.lang.NullPointerException”?

时间:2017-11-29 07:01:43

标签: java android firebase

我有两个按钮发送到两个不同的活动(它们用于填充  表格并提交)。

我正在使用这两个活动的代码(有一些更改),但其中一个可用于其他CRASHES。 这是工作的代码

public class InstaForm extends AppCompatActivity {

//
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
//

private EditText mUserId, mPassword;
private Button mAdd, mModify;

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

    //
    mAuth = FirebaseAuth.getInstance();
    //

    mUserId = (EditText) findViewById(R.id.userIdInsta);
    mPassword = (EditText) findViewById(R.id.passwordInsta);
    mAdd = (Button) findViewById(R.id.btnInsta);
    mModify = (Button) findViewById(R.id.btnModifyInsta);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference mRef = database.getReference();

    mAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String userId = mUserId.getText().toString();
            String password = mPassword.getText().toString();

            String user_id_auth = mAuth.getUid();
            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id_auth).child("Insta");

            Map newPost = new HashMap();
            newPost.put("IdOfUser", userId);
            newPost.put("Password", password);

            current_user_db.setValue(newPost).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(InstaForm.this, "Error! Please try again.", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(InstaForm.this, "SUCCESSFULLY REGISTERED!", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(InstaForm.this, SocialOptionsActivity.class);
                        startActivity(intent);
                        finish();
                        return;
                    }
                }
            });


        }
    });

    mModify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

}

而这一个正在崩溃

public class TwitterForm extends AppCompatActivity {

//
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
//

private EditText mUserId, mPassword;
private Button mAdd, mModify;

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

    //
    mAuth = FirebaseAuth.getInstance();
    //

    mUserId = (EditText) findViewById(R.id.userIdTwitter);
    mPassword = (EditText) findViewById(R.id.passwordTwitter);
    mAdd = (Button) findViewById(R.id.btnTwitter);
    mModify = (Button) findViewById(R.id.btnModifyTwitter);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference mRef = database.getReference();

    mAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String userId = mUserId.getText().toString();
            String password = mPassword.getText().toString();

            String user_id_auth = mAuth.getUid();
            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id_auth).child("Twitter");

            Map newPost = new HashMap();
            newPost.put("IdOfUser", userId);
            newPost.put("Password", password);

            current_user_db.setValue(newPost).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(TwitterForm.this, "Error! Please try again.", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(TwitterForm.this, "SUCCESSFULLY REGISTERED!", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(TwitterForm.this, SocialOptionsActivity.class);
                        startActivity(intent);
                        finish();
                        return;
                    }
                }
            });
        }
    });

    mModify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

}

我的logcat是这个

11-29 12:16:02.193 4588-4588/com.example.android.snapgrowth7 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.android.snapgrowth7, PID: 4588
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.snapgrowth7/com.example.android.snapgrowth7.TwitterForm}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

                                                                               at com.example.android.snapgrowth7.TwitterForm.onCreate(TwitterForm.java:48)
                                                                               at android.app.Activity.performCreate(Activity.java:6672)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6123) 

logcat中间隔部分之后的第一行,表示错误位于第二个代码的第48行,但我无法弄清楚我做错了什么。

有人可以建议吗? 感谢

0 个答案:

没有答案