我目前正在尝试在Android中学习SQLite,并在互联网上学习了一些教程
现在我遇到了这个错误,我希望有人会指出我的错误。
我不确定导致这个问题的原因,我已经用与我相同的问题阅读了大部分这些主题,但我仍然无法弄清楚如何修复它。
这是MainActivity.java
package com.nyonyo.sqlitetest;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
EditText firstname, lastname, marks, id;
Button btnAdd;
Button btnDel;
Button btnView;
Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB = new DatabaseHelper();
firstname = (EditText) findViewById(R.id.editFirstName);
lastname = (EditText) findViewById(R.id.editLastName);
marks = (EditText) findViewById(R.id.editMarks);
id = (EditText) findViewById(R.id.edit_id);
btnAdd = (Button) findViewById(R.id.button_add);
btnDel = (Button) findViewById(R.id.button_delete);
btnView = (Button) findViewById(R.id.button_viewAll);
btnUpdate = (Button) findViewById(R.id.button_update);
addData();
deleteData();
viewData();
updateData();
}
private void addData() {
btnAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
boolean isInserted = myDB.insertData(firstname.getText().toString(),
lastname.getText().toString(),
marks.getText().toString());
if (isInserted)
Toast.makeText(MainActivity.this,"Data Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"Insert Data Failed", Toast.LENGTH_SHORT).show();
}
});
}
private void deleteData() {
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer delRow = myDB.deleteData(id.getText().toString());
if(delRow > 0)
Toast.makeText(MainActivity.this,"Row Deleted",Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"Delete Failed", Toast.LENGTH_SHORT).show();
}
});
}
private void viewData() {
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = myDB.getData();
if(res.getCount()==0){
showMessage("Error","No Data Found");
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID :"+ res.getString(0)+"\n");
buffer.append("First Name :"+ res.getString(1)+"\n");
buffer.append("last Name :"+ res.getString(2)+"\n");
buffer.append("Marks :"+ res.getString(3)+"\n\n");
}
}
});
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
private void updateData() {
boolean isUpdate = myDB.updateData(id.getText().toString(),
firstname.getText().toString(),
lastname.getText().toString(),
marks.getText().toString());
if(isUpdate)
Toast.makeText(MainActivity.this,"Database Updated", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"Update Failed", Toast.LENGTH_SHORT).show();
}
}
这是DatabaseHelper.java
package com.nyonyo.sqlitetest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by NyoNyo on 07/08/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "Student_table";
public static final String COL_ID = "ID";
public static final String COL_FIRSTNAME = "Firstname";
public static final String COL_LASTNAME = "Lastname";
public static final String COL_MARKS = "Marks";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table" + TABLE_NAME +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"FIRSTNAME TEXT," +
"LASTNAME TEXT," +
"MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST" + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String firstname, String lastname, String marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_FIRSTNAME,firstname);
values.put(COL_LASTNAME,lastname);
values.put(COL_MARKS,marks);
long result = db.insert(TABLE_NAME,null,values);
if(result == -1)
return false;
else
return true;
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM"+ TABLE_NAME,null);
return res;
}
public boolean updateData(String id, String firstname, String lastname, String marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_ID, id);
values.put(COL_FIRSTNAME, firstname);
values.put(COL_LASTNAME, lastname);
values.put(COL_MARKS, marks);
db.update(TABLE_NAME, values, "ID = ?", new String[] {id});
return true;
}
public Integer deleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME,"ID = ?", new String[] {id});
}
}
当我尝试运行该应用时,会出现此错误。
Error:(28, 16) error: constructor DatabaseHelper in class DatabaseHelper cannot be applied to given types;
required: Context
found: no arguments
reason: actual and formal argument lists differ in length
这是我关注的教程 http://www.codebind.com/android-tutorials-and-examples/android-sqlite-tutorial-example/
提前致谢。
这是Stack Trace吗?不太确定。
08-08 09:50:52.968 22463-22463/? I/art: Late-enabling -Xcheck:jni08-08 09:50:53.429 22463-22463/com.nyonyo.sqlitetest W/System: ClassLoader referenced unknown path: /data/app/com.nyonyo.sqlitetest-1/lib/x8608-08 09:50:53.458 22463-22463/com.nyonyo.sqlitetest I/InstantRun: starting instant run server: is main process08-08 09:50:53.715 22463-22463/com.nyonyo.sqlitetest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable08-08 09:50:54.018 22463-22463/com.nyonyo.sqlitetest D/AndroidRuntime: Shutting down VM08-08 09:50:54.020 22463-22463/com.nyonyo.sqlitetest E/AndroidRuntime: FATAL EXCEPTION: main Process: com.nyonyo.sqlitetest, PID: 22463 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyonyo.sqlitetest/com.nyonyo.sqlitetest.MainActivity}: 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:2684)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
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.nyonyo.sqlitetest.MainActivity.addData(MainActivity.java:48)
at com.nyonyo.sqlitetest.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6684)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 08-08 09:50:54.055 22463-22463/com.nyonyo.sqlitetest I/Process: Sending signal. PID: 22463 SIG: 9
答案 0 :(得分:0)
在您的MainActivity中,您错过了this
。这就是为什么你会得到 实际和正式的参数列表长度 错误。
myDB = new DatabaseHelper(this);
你也错过了setContentView(R.layout.activity_main);
。将其放在super.onCreate(savedInstanceState);