gpt4 book ai didi

java - 尝试创建 SQLite 数据库时出现 NullPointerException - Android

转载 作者:行者123 更新时间:2023-12-02 04:33:03 25 4
gpt4 key购买 nike

我是 Android 新手,正在学习创建“SQLite 数据库”。当我执行应用程序时,出现NullPointerException` 错误。有人可以帮我解决这个问题吗?谢谢

这是我的代码。

添加时间表类

public class Addschedule extends ActionBarActivity {

ScheduleAdapater schedulehelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addschedule);

schedulehelper = new ScheduleAdapater(this);

Button bt = (Button) findViewById(R.id.btn_add_schedule);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addschedule();
}
});
}


public void addschedule(){

EditText et_id_1 = (EditText) findViewById(R.id.et_id);
EditText et_name_1 = (EditText) findViewById(R.id.et_name);
EditText et_date_1 = (EditText) findViewById(R.id.et_date);

int sch_id = Integer.parseInt(String.valueOf(et_id_1.getText()));
String name = et_name_1.getText().toString();
String date = et_date_1.getText().toString();

long id = schedulehelper.InsertData(sch_id, name, date);
if(id<0){
Message.message(this, "Unsuccessful insert");
}else{
Message.message(this, "successful insert");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_addschedule, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


}

调度适配器类

public class ScheduleAdapater {

scheduleHelper sch_helper;

public ScheduleAdapater(Context context){sch_helper = new scheduleHelper(context); }

public long InsertData(Integer id, String name, String date){

SQLiteDatabase db = sch_helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(scheduleHelper.SCH_ID, id);
contentValues.put(scheduleHelper.SCH_NAME, name);
contentValues.put(scheduleHelper.SCH_DATE, date);
long result_id = db.insert(scheduleHelper.TABLE_NAME, null, contentValues);
return result_id;
}


static class scheduleHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "certisagent";
private static final String TABLE_NAME = "schedule";
private static final int DATABASE_VERSION = 3;
private static final String SCH_ID="schedule_id";
private static final String SCH_NAME = "schedule_name";
private static final String SCH_DATE="schedule_date";
private Context context;


private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+SCH_ID+" " +
"INTEGER PRIMARY KEY, "+SCH_NAME+" VARCHAR(255), "+SCH_DATE+" VARCHAR(50));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_NAME+";";


public scheduleHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
try {
Message.message(context, "Oncreate called");
db.execSQL(CREATE_TABLE);
}catch (SQLException e){
Message.message(context, ""+e);

}

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "onupgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (SQLException e){
Message.message(context, ""+e);

}
}
}
}

消息类

public class Message {
public static void message(Context context, String message){
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}

错误堆栈

07-03 12:27:35.630    4482-4482/lk.db.learn.learndb E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lk.db.learn.learndb, PID: 4482
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:106)
at android.widget.Toast.makeText(Toast.java:264)
at lk.db.learn.learndb.Message.message(Message.java:11)
at lk.db.learn.learndb.ScheduleAdapater$scheduleHelper.onUpgrade(ScheduleAdapater.java:66)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at lk.db.learn.learndb.ScheduleAdapater.InsertData(ScheduleAdapater.java:21)
at lk.db.learn.learndb.Addschedule.addschedule(Addschedule.java:48)
at lk.db.learn.learndb.Addschedule$1.onClick(Addschedule.java:28)
at android.view.View.performClick(View.java:4496)
at android.view.View$PerformClick.run(View.java:18603)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

有人可以帮我解决这个问题吗?谢谢。

最佳答案

context=null 位于

 Message.message(context, "Oncreate called");

所以在使用之前初始化

 public scheduleHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}

scheduleHelper类中

关于java - 尝试创建 SQLite 数据库时出现 NullPointerException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com