我即将制作Android应用程序,但由于致命 EXCEPTION:应用程序未保存的主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)
请帮助我,因为我已经尝试了所有可能的组合。我会 非常感谢你们所有人,并感谢你们。
答案 0 :(得分:3)
首先使用 布尔 而不是 布尔 ,并尝试检查您的 getIntent() 在从意图接收包之前未返回 null 意图。