gpt4 book ai didi

java - Eclipse 数据未插入 SQLite 数据库并崩溃

转载 作者:行者123 更新时间:2023-11-30 11:27:02 26 4
gpt4 key购买 nike

当我单击该按钮时,它应该将键入的数据连同从单独的 Activity 中收集的经度和纬度一起存储到数据库中。我不确定我做错了什么,我的其他数据库工作正常。任何帮助,将不胜感激。我将在下面发布代码:

这是 Activity java。

public class NoteActivity extends Activity implements OnClickListener {

Button buttonLeaveNote;

private EditText mTitle;
private EditText mDesc;

protected NoteDBHelper noteDB = new NoteDBHelper(NoteActivity.this);

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

buttonLeaveNote = (Button) findViewById(R.id.buttonLeaveNote);
buttonLeaveNote.setOnClickListener(this);

mTitle = (EditText)findViewById(R.id.etitle);
mDesc = (EditText)findViewById(R.id.edesc);
}

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

@Override
public void onClick(View v) {
switch(v.getId()){

case R.id.buttonLeaveNote:

String title = mTitle.getText().toString();
String desc = mDesc.getText().toString();
Intent intent = getIntent();
String lati = intent.getExtras().getString("lati");
String lng = intent.getExtras().getString("lng");

boolean invalid = false;

if(title.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter a title", Toast.LENGTH_SHORT).show();
}
else
if(desc.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter description", Toast.LENGTH_SHORT).show();

}

else

if(invalid == false)
{
addEntry(title, desc, lati, lng);
Intent i_note = new Intent(NoteActivity.this, JustWanderingActivity.class);
startActivity(i_note);
//finish();
}

break;
}
}

public void onDestroy()
{
super.onDestroy();
noteDB.close();
}

private void addEntry(String title, String desc, String lati, String lng)
{
SQLiteDatabase notedb = noteDB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("desc", desc);
values.put("lati", lati);
values.put("lng", lng);
try
{
long newRowId;
newRowId = notedb.insert(NoteDBHelper.DATABASE_TABLE_NAME, null, values);

Toast.makeText(getApplicationContext(), "Note successfully added", Toast.LENGTH_SHORT).show();
}

catch(Exception e)
{
e.printStackTrace();
}
}
}

这是 SQLite 数据库 Java

public class NoteDBHelper extends SQLiteOpenHelper 

{
private SQLiteDatabase notedb;

public static final String NOTE_ID = "_nid";
public static final String NOTE_TITLE = "title";
public static final String NOTE_DESC = "desc";
public static final String NOTE_LAT = "lati";
public static final String NOTE_LONG = "lng";


NoteDBHelper noteDB = null;
private static final String DATABASE_NAME = "stepsaway.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "notes";


private static final String DATABASE_TABLE_CREATE =
"CREATE TABLE" + DATABASE_TABLE_NAME + "(" +
"_nid INTEGER PRIMARY KEY AUTOINCREMENT," +
"title TEXT NOT NULL, desc LONGTEXT NOT NULL, lati TEXT NOT NULL, lng, TEXT NOT NULL);";

public NoteDBHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}


@Override
public void onCreate(SQLiteDatabase notedb) {

try{

notedb.execSQL(DATABASE_TABLE_CREATE);


}catch(Exception e){
e.printStackTrace();
}
}



@Override
public void onUpgrade(SQLiteDatabase notedb, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}




public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}




public void open() {

getWritableDatabase();
}


public Cursor getDetails(String text) throws SQLException
{

Cursor mCursor =
notedb.query(true, DATABASE_TABLE_NAME,
new String[]{NOTE_ID, NOTE_TITLE, NOTE_DESC, NOTE_LAT, NOTE_LONG},
NOTE_TITLE + "=" + text,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;


}
}

这是日志

FATAL EXCEPTION: main
java.lang.NullPointerException
atcom.example.stepsaway.NoteActivity.onClick(NoteActivity.java:53)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

这是 putExtra() 调用。

    tvLatitude.setText("Latitude:" +  location.getLatitude());
tvLongitude.setText("Longitude:" + location.getLongitude());
String lati = tvLatitude.getText().toString();
String lng = tvLongitude.getText().toString();
Intent i = new Intent(this, NoteActivity.class);
i.putExtra("lati", lati);
i.putExtra("lng", lng);

最佳答案

没有 logcat 很难帮助你...所以也发布 logcat

一个错误是在您的 NoteDBHelper 类 中创建表查询...

您忘记在 keyword table 和表名 (DATABASE_TABLE_NAME) 之间放置空格>

所以要么改为:

private static final String DATABASE_TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
"_nid INTEGER PRIMARY KEY AUTOINCREMENT," +
"title TEXT NOT NULL, desc LONGTEXT NOT NULL, lati TEXT NOT NULL, lng, TEXT NOT NULL);";

或者:

public static final String DATABASE_TABLE_NAME = " notes";

附言。

NullPointerException

在使用 intent.getExtras() 之前检查 null 并确保您传递的是 String 而不是 doubleintent.putExtra()

intent.putExtra("lati","CheckHere it must be String not double");
intent.putExtra("lng","CheckHere it must be String not double");

下面的代码也可以避免崩溃并帮助您正确调试

Intent intent = getIntent();
if(intent != null)
{
String lati = intent.getExtras().getString("lati");
String lng = intent.getExtras().getString("lng");
if(lati == null)
{
Log.e("Checking lati","Its Null");
lati="0";
}
if(lng == null)
{
Log.e("Checking lng","Its Null");
lng="0";
}
}
else
{
Log.e("Checking intent","Its Null");
}

关于java - Eclipse 数据未插入 SQLite 数据库并崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510864/

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