所以我在第87和97行周围的这段代码出现错误。这是代码
package username.myandroidproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
Button addButton;
Button button;
Button usefulLinks;
Button button2;
EditText BookText;
EditText authorView;
EditText categoryView;
MyDBhandler myDB;
String[] bookNames;
String[] bookAuthor;
String[] bookCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BookText = findViewById(R.id.BookText);
authorView = findViewById(R.id.authorView);
categoryView = findViewById(R.id.categoryView);
addButton = findViewById(R.id.addButton);
//begining of button stuff for navigation
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openactivity2();
}
});
usefulLinks = findViewById(R.id.usefulLinks);
usefulLinks.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openusefullinks();
}
});
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openlistact();
}
});
//end of buttons
}
public void openusefullinks() {
Intent intent = new Intent(this, usefulWebsites.class);
startActivity(intent);
}
public void openactivity2() {
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
public void openlistact() {
Intent intent = new Intent(this, listact.class);
startActivity(intent);
}
public void addButtonClicked(View v){
String newEntry = BookText.getText().toString();
String newEntry2 = authorView.getText().toString();
String newEntry3 = categoryView.getText().toString();
if(BookText.length() != 0) {
AddData(newEntry, newEntry2, newEntry3);
BookText.setText("");
}
else {
Toast.makeText(MainActivity.this, "Please enter text",Toast.LENGTH_LONG).show();
}
}
public void AddData(String newEntry, String newEntry2, String newEntry3){
boolean insertData = myDB.addData(newEntry, newEntry2, newEntry3);
if(insertData==true){
Toast.makeText(MainActivity.this, "Its done boi",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "error",Toast.LENGTH_LONG).show();
}
}
}
这是错误代码
由于:java.lang.NullPointerException:尝试调用虚拟 方法'布尔值 username.myandroidproject.MyDBhandler.addData(java.lang.String, null对象引用上的java.lang.String,java.lang.String) 在username.myandroidproject.MainActivity.AddData(MainActivity.java:97) username.myandroidproject.MainActivity.addButtonClicked(MainActivity.java:87)
public boolean addData(String BOOKNAME, String AUTHOR, String CATEGORY) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, BOOKNAME);
contentValues.put(COL3, AUTHOR);
contentValues.put(COL4, CATEGORY);