大家晚上好,我認爲,在我的Android數據庫中提取價值內容。從SQL數據庫中提取數據沒有成功
首先,我嘗試了一個設備上的應用程序(SGS3)和數據庫正確創建和popoulated查詢,但是當我嘗試從光標值調用提取,LogCat給我錯誤NullPointerExeption 。我已經閱讀了一些其他的線程,但我還沒有找到我的問題的答案。
這是創建數據庫,查詢和數據的位置代碼:
public static Cursor fetchProducts(){ //attenzione cambioato,messo static
return mDb.query(PositionsMetaData.POSITIONS_TABLE, null,null,null,null,null,null);
}
static class PositionsMetaData {
static final String POSITIONS_TABLE = "position";
static final String ID = "_id";
static final String POSITIONS_NAME_KEY = "name";
static final String POSITIONS_LATITUDE_KEY = "lat";
static final String POSITIONS_LONGITUDE_KEY = "lon";
}
private static final String POSITIONS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ PositionsMetaData.POSITIONS_TABLE + " ("
+ PositionsMetaData.ID + " integer primary key autoincrement, "
+ PositionsMetaData.POSITIONS_NAME_KEY + " text, "
+ PositionsMetaData.POSITIONS_LATITUDE_KEY + " double not null, "
+ PositionsMetaData.POSITIONS_LONGITUDE_KEY + " double not null); ";
說不上來爲什麼,但是Eclipse希望光標與靜態,我不知道這爲什麼這,順便說一句,THI是數據的提取代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listagps);
TextView nomelista=(TextView)findViewById(R.id.nomelista);
TextView latitudinelista=(TextView)findViewById(R.id.latitudinelista);
TextView longitudinelista=(TextView)findViewById(R.id.longitudinelista);
// CREAZIONE LISTA PUNTI GPS SU LAYOUT LISTAGPS
ListView posizionigps=(ListView)findViewById(R.id.listagps);
final listagpsdb db = new listagpsdb(getApplicationContext());
db.open();
Cursor c=listagpsdb.fetchProducts();
startManagingCursor(c);
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter=new SimpleCursorAdapter (
this,
R.layout.listagps_item,
c,
new String[]{listagpsdb.PositionsMetaData.POSITIONS_NAME_KEY,listagpsdb.PositionsMetaData.POSITIONS_LONGITUDE_KEY,listagpsdb.PositionsMetaData.POSITIONS_LATITUDE_KEY},
new int []{R.id.nomelista,R.id.longitudinelista,R.id.latitudinelista});
posizionigps.setAdapter(adapter);
int nomecol=c.getColumnIndex(listagpsdb.PositionsMetaData.POSITIONS_NAME_KEY);
int loncol=c.getColumnIndex(listagpsdb.PositionsMetaData.POSITIONS_LONGITUDE_KEY);
int lancol=c.getColumnIndex(listagpsdb.PositionsMetaData.POSITIONS_LATITUDE_KEY);
if(c.moveToFirst()){
do {
nomelista.append(c.getString(nomecol));
latitudinelista.append(c.getString(lancol));
longitudinelista.append(c.getString(loncol));
} while (c.moveToNext());
}
db.close();
}
}
的logcat的給我nomelista,latitudinelista和longitudinelista,所有空但在DB都填充錯誤。
有沒有解決這個問題的建議?是不正確的光標管理還是因爲textview沒有設置數據?我已經試過我知道的永恆,但沒有成功。
非常感謝所有的想幫助我:)
問題的FULL logcat的
05-05 14:42:22.613: E/AndroidRuntime(988): FATAL EXCEPTION: main
05-05 14:42:22.613: E/AndroidRuntime(988): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.drive.info/com.drive.info.listagps}: java.lang.NullPointerException
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.os.Looper.loop(Looper.java:137)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-05 14:42:22.613: E/AndroidRuntime(988): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:42:22.613: E/AndroidRuntime(988): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 14:42:22.613: E/AndroidRuntime(988): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-05 14:42:22.613: E/AndroidRuntime(988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-05 14:42:22.613: E/AndroidRuntime(988): at dalvik.system.NativeStart.main(Native Method)
05-05 14:42:22.613: E/AndroidRuntime(988): Caused by: java.lang.NullPointerException
05-05 14:42:22.613: E/AndroidRuntime(988): at com.drive.info.listagps.onCreate(listagps.java:68)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.Activity.performCreate(Activity.java:5008)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-05 14:42:22.613: E/AndroidRuntime(988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-05 14:42:22.613: E/AndroidRuntime(988): ... 11 more
請在這裏添加您的完整logcat。 – Sajmon 2013-05-05 15:01:06
在主帖中添加完整的logcat :)第68行是nomelista.append(c.getString(nomecol))的行; – 2013-05-05 15:12:14
看起來像TextView nomelista爲null。 R.layout.listagps中的nomelista是否被污染? – bakriOnFire 2013-05-05 15:39:53