当前位置: 首页 > 知识库问答 >
问题:

尝试对空对象引用调用虚拟方法“boolean java.lang.Boolean.BooleanValue()”

淳于熙云
2023-03-14

我将要制作android应用程序,但是由于致命的异常:main java.lang.NullPointerException应用程序没有保存数据,请在这里帮助我

这是我的EditNoteActivity

    package com.example.notepad.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;


   public class EditNoteActivity extends ActionBarActivity implements View.OnClickListener {
    String A, B;
    int id;
    Boolean isUpdate = false;
    Button Save, Delete;
    private Toolbar toolbar;
    EditText edit;
    EditText edit1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_notes);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        EditText edit = (EditText) this.findViewById(R.id.editText2);
        EditText edit1 = (EditText) this.findViewById(R.id.editText);
        A = edit.getText().toString();
        B = edit1.getText().toString();

        Note note = new Note();
        note.setDescription(A);
        note.setTitle(B);
        Save = (Button) findViewById(R.id.button);
        Save.setOnClickListener(this);
        Delete = (Button) findViewById(R.id.button2);
        Delete.setOnClickListener(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey("source")) {
            if (bundle.getString("source").equalsIgnoreCase("editpress")) {
                isUpdate = true;
                id = bundle.getInt("noteId");
                A = bundle.getString("noteTitle");
                B = bundle.getString("noteDescription");
            } else if (bundle.getString("source").equalsIgnoreCase("addPress")) {
                isUpdate = false;

            } else {
                Toast.makeText(this, "invalid argument", Toast.LENGTH_SHORT).show();
                super.onBackPressed();
            }
        }
    }

    public void Save() {
        A = edit.getText().toString();
        B = edit1.getText().toString();

        if (!isvalid()) {
            return;
        }
        Note note = new Note();
        note.setTitle(A);
        note.setDescription(B);
        Databasehandler data = new Databasehandler(this);
        if (!isUpdate) {
            data.addnote(note);
            Toast.makeText(this, "notes added sucessfully", Toast.LENGTH_LONG).show();
        } else {

        }
        List<Note> list = data.getAllNotes();
        MainActivity.adapt.clear();
        MainActivity.adapt.addAll(list);
        MainActivity.adapt.notifyDataSetChanged();
        super.onBackPressed();

    }

    public void Delete() {

    }

    public boolean isvalid() {
        if (A.isEmpty() && A != null && B.isEmpty() && B != null) {
            Toast.makeText(this, "please enter tittle and description", Toast.LENGTH_SHORT).show();
            return false;
        } else if (A.isEmpty() && A != null) {
            Toast.makeText(this, "please enter title", Toast.LENGTH_SHORT).show();
            return false;
        } else if (B.isEmpty() && B != null) {
            Toast.makeText(this, "please enter description", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                Save();
                break;

        }

    }
}

这是我的Logcat错误

E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.example.notepad.myapplication, PID: 30744
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad.myapplication/com.example.notepad.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
                                                   at com.example.notepad.myapplication.BlankFragment.setup(BlankFragment.java:64)
                                                   at com.example.notepad.myapplication.MainActivity.onCreate(MainActivity.java:36)
                                                   at android.app.Activity.performCreate(Activity.java:6662)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:154) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

共有1个答案

楚羽
2023-03-14

首先,使用boolean而不是boolean,并在从intent接收bundle之前,尝试检查getIntent()是否没有返回null intent。

 类似资料: