我正在创建一个Android应用。在里面;


创建一个用户
成功创建用户后,该应用将显示另一个活动


身份验证和帐户创建工作正常。

我创建的第二个活动有一个微调器。此微调器由多余的nameDoctor中的值填充。

微调器第一次工作。但是,如果我按“后退”按钮或单击任何其他按钮(在添加微调器之前曾经起作用),应用程序将崩溃,并显示NullPointerException。

微调器应收到一个非空值,因为它会收到该值。但是我不确定NPE来自哪里。

如果有人可以帮助我,我是android应用程序开发的新手。

    //code
    private void registerMedecin() {
       //code
        mAuth.createUserWithEmailAndPassword(doctor.email, password).addOnCompleteListener(new
        OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (!task.isComplete()) {
                    Toast.makeText(Account.this, "Account creation error: " +
        task.getException().getMessage(), Toast.LENGTH_LONG).show();
                    Log.e("doctor", "Account creation error: " + task.getException().getMessage());
                } else {

                    Log.e("Adddoctor", "user account created");

                    Intent sendNameIntent = new Intent(Compte.this, rdv_detail.class);
                    String name=nameDoctor.getText().toString();
                    sendNameIntent.putExtra("nameDoctor",name);
                    startActivity(sendNameIntent);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SaveDoctorData(doctor);




                        }
                    }).start();
                }
            }
        });
    }


rdv_detail.java

    //code
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rdv_detail);

        spinner_doc =findViewById(R.id.spinner_doctor);

        String nameExtra = getIntent().getStringExtra("nameDoctor");
        ArrayList<String> spinnerData = new ArrayList<String>();
        spinnerData.add(nameExtra);

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(rdv_detail.this,
              android.R.layout.simple_spinner_item, spinnerData);

        spinner_doc.setAdapter(arrayAdapter);




}


除错

     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:457)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:419)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
        at android.widget.Spinner.onMeasure(Spinner.java:614)
        at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:428)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1204)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:723)
        at android.view.View.measure(View.java:24530)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:742)
        at android.view.View.measure(View.java:24530)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3006)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1833)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:725)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 22413 SIG: 9
Disconnected from the target VM, address: 'localhost:8624', transport: 'socket'

最佳答案

由tryng导致将空对象转换为字符串
String nameExtra = getIntent().getStringExtra("nameDoctor");

请在传递nameExtra到数组之前检查。

如果不是null,请对此答案发表评论。

当同一用户重新连接时,String name=nameDoctor.getText().toString();应该为空。请先检查,然后再传递给其他人员。

关于java - 在ArayAdapter中使用Spinner在空对象引用上调用'java.lang.String java.lang.Object.toString()',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61895094/

10-09 01:46