我是数据库中的新手。我正在尝试创建一个应用程序,将任何已发送的SMS(安装此应用程序后)从SentBox插入SQLite
数据库。如果make Toast而不是将细节插入数据库,它可以正常工作。但是当我尝试将其插入数据库时,它会给出NullPointerException
。
LogCat错误:
09-29 13:19:54.999: E/AndroidRuntime(3059): FATAL EXCEPTION: main
09-29 13:19:54.999: E/AndroidRuntime(3059): Process: com.manish.smshistory, PID: 3059
09-29 13:19:54.999: E/AndroidRuntime(3059): java.lang.NullPointerException
09-29 13:19:54.999: E/AndroidRuntime(3059): at com.manish.smshistory.MainActivity.getSMSDetails(MainActivity.java:95)
09-29 13:19:54.999: E/AndroidRuntime(3059): at com.manish.smshistory.MainActivity$1.onClick(MainActivity.java:41)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.view.View.performClick(View.java:4562)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.view.View$PerformClick.run(View.java:18918)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.os.Handler.handleCallback(Handler.java:808)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.os.Handler.dispatchMessage(Handler.java:103)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.os.Looper.loop(Looper.java:193)
09-29 13:19:54.999: E/AndroidRuntime(3059): at android.app.ActivityThread.main(ActivityThread.java:5388)
09-29 13:19:54.999: E/AndroidRuntime(3059): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 13:19:54.999: E/AndroidRuntime(3059): at java.lang.reflect.Method.invoke(Method.java:515)
09-29 13:19:54.999: E/AndroidRuntime(3059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 13:19:54.999: E/AndroidRuntime(3059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
09-29 13:19:54.999: E/AndroidRuntime(3059): at dalvik.system.NativeStart.main(Native Method)
数据库:
public class SMSSent extends SQLiteOpenHelper {
public SMSSent(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Sent_SMS_Table (Id INTEGER PRIMARY KEY AUTOINCREMENT, Source TEXT, Dttm TEXT, Msg TEXT, Type TXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
主要活动:
public class MainActivity extends Activity {
TextView textView;
SMSSent st;
Button Save, Next;
String body, dttm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Save = (Button)findViewById(R.id.Save);
Save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getSMSDetails();
}
});
Next = (Button)findViewById(R.id.Next);
Next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, RetrieveSMS.class));
}
});
} // onCreate ends
public void getSMSDetails() {
// TODO Auto-generated method stub
Uri uri = Uri.parse("content://sms/sent");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
body = cursor.getString(cursor.getColumnIndexOrThrow("body"))
.toString();
String date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
.toString();
Date smsDayTime = new Date(Long.valueOf(date));
dttm = smsDayTime.toString();
SQLiteDatabase db=st.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("Source", "Self");
values.put("Dttm", dttm);
values.put("Msg", body);
values.put("Type", "OUT");
db.insert("Sent_SMS_Table", "Source", values);
db.close();
cursor.moveToNext();
}
}
cursor.close();
}
} // Activity ends
答案 0 :(得分:1)
看起来问题与
有关SQLiteDatabase db=st.getWritableDatabase();
试
SMSSent st = new SMSSent();
SQLiteDatabase db=st.getWritableDatabase();
希望这有帮助
答案 1 :(得分:0)
嗨问题是如果你想使用autoincrement id只是在代码下面使用自动增量关键字
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Sent_SMS_Table (id INTEGER PRIMARY KEY, Source TEXT, Dttm TEXT, Msg TEXT, Type TEXT);");
}