2014-01-15 35 views
0

我的下一個問題。NullpointerException ListView中的SQLite數據

我想在列表視圖中顯示數據,我得到一個Nullpointerexception。我的數據源是一個SQLite數據庫。在下面的文本中是Activity和LogCat。

請幫幫我!

public class ListHaltestelleActivity extends Activity { 
private SimpleAdapter simpleAdapter; 
private SQLiteDatabase db; 
protected static final String TAG = "DataAdapter"; 

private SQLiteDatabase mDb; 
DatabaseHelper myDbHelper = new DatabaseHelper(this); 
    //put the table name and column in constants 
    public static final String COLUMN_NAME = "scotty_haltestelle"; 
    Intent intent; 

. 
. 
. 
. 

public void setListView() 
{ 
    try 
    { 
     myDbHelper.getReadableDatabase(); 
     // Alle Daten der Datenbank abrufen mithilfe eines Cursors 
     Cursor cursor = db.rawQuery("Select name from scotty_haltestelle",null); 

     cursor.moveToFirst(); 
     ListView listview = (ListView)findViewById(R.id.listView); 
     List<Map<String, String>> haltestellenList = new ArrayList<Map<String, String>>(); 

     while (cursor.moveToNext()){ 
      //in this string we get the record for each row from the column "name" 
      String name = cursor.getString(0); 

      String outPut = name; 
      haltestellenList.add(createHaltestelle("haltestelle", outPut)); 
     } 

     simpleAdapter = new SimpleAdapter(ListHaltestelleActivity.this, haltestellenList, 
       android.R.layout.simple_list_item_1, 
       new String[] { "haltestelle" }, new int[] { android.R.id.text1 }); 
     listview.setAdapter(simpleAdapter); 

     //here we close the cursor because we do not longer need it 
     //} 
     cursor.close(); 
    } 
    catch (SQLException mSQLException) 
    { 
     Log.e(TAG, "getTestData >>"+ mSQLException.toString()); 
     throw mSQLException; 
    } 
} 

// Hash - Map in der die Haltestelle mit Name und dann Number gespeichert wird 
    private HashMap<String, String> createHaltestelle(String name, String number) { 
     HashMap<String, String> haltestelleNameNo = new HashMap<String, String>(); 
     haltestelleNameNo.put(name, number); 

     return haltestelleNameNo; 

這裏是logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.ListHaltestelleActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
      at android.app.ActivityThread.access$700(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4998) 
      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:777) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at at.atn.android.ListHaltestelleActivity.setListView(ListHaltestelleActivity.java:76) 
      at at.atn.android.ListHaltestelleActivity.onCreate(ListHaltestelleActivity.java:61) 
      at android.app.Activity.performCreate(Activity.java:5243) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
             

感謝您的幫助

回答

1

而不是

myDbHelper.getReadableDatabase(); 

db=myDbHelper.getReadableDatabase(); 
1

你忘了分配一個值db成員變量,拋出的myDbHelper.getReadableDatabase()結果。這導致NPE爲db未初始化。

後面的代碼中,您將跳過第一行,因爲moveToFirst()後面跟着moveToNext()

+0

是啊,我知道了。謝謝你的幫助! – schocka94

相關問題