2011-10-14 64 views
0

新的Android和Java真的(我最初是一名Pascal程序員)並且遇到了問題 - 我的應用程序正在關閉,我無法解決原因。Android部隊關閉錯誤

我只是試圖調用一個活動來顯示我的(當前爲空)數據庫中的數據表。我正在使用'學習Android'書籍,並且大部分代碼都被剪掉並粘貼了。我已將所有數據庫訪問例程重構爲特定的類,但似乎是一個問題。

調用方法:

@Override 
public void onResume() { 
    super.onResume(); 

    Log.i(TAG, "In onResume..."); 

    // Link to WYWHApplication module 
    WYWHApplication wywh = (WYWHApplication) this.getApplication(); 

    // Fetch trip list 
    Cursor tripList = wywh.getBasicList(); 

,這是DB類:

public class WYWHApplication extends Application { 

private static final String TAG = WYWHApplication.class.getSimpleName(); 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.i(TAG, "In onCreate for app..."); 
} 

@Override 
public void onTerminate() { 
    super.onTerminate(); 
    Log.i(TAG, "In onTerminate for app..."); 
} 

// Link to DB class 
private DatabaseUtils dbUtils; 

// Accessor method - how other modules get access to DB. 
public DatabaseUtils getDBData() { 
    return dbUtils; 
} 

public synchronized Cursor getBasicList() { 

    Log.i(TAG, "Getting basic list of trips"); 

    // Get list of DB entries 
    Cursor tripEntries = this.getDBData().fetchList("TRIP_TABLE"); 

    Log.i(TAG, "...returned from getting basic list of trips"); 

    return tripEntries; 
} 

的logcat的有以下幾點:

10-14 20:38:29.543: INFO/WYWHApplication(305): In onCreate for app... 
10-14 20:38:29.603: DEBUG/WYWH(305): Start of onCreate.... 
10-14 20:38:30.283: DEBUG/WYWH(305): ...end of onCreate. 
10-14 20:41:05.943: INFO/ActivityManager(58): Starting activity: Intent { cmp=co.uk.sanetech.wywh/.TripsScreenActivity } 
10-14 20:41:06.183: INFO/Trips(305): Start of onCreate.... 
10-14 20:41:06.773: INFO/Trips(305): In onResume... 
10-14 20:41:06.773: INFO/WYWHApplication(305): Getting basic list of trips 
10-14 20:41:06.793: DEBUG/AndroidRuntime(305): Shutting down VM 
10-14 20:41:06.793: WARN/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305): FATAL EXCEPTION: main 
10-14 20:41:06.993: ERROR/AndroidRuntime(305): java.lang.RuntimeException: Unable to resume activity {co.uk.sanetech.wywh/co.uk.sanetech.wywh.TripsScreenActivity}: java.lang.NullPointerException 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128) 

(左位在這裏)

10-14 20:41:06.993: ERROR/AndroidRuntime(305): Caused by: java.lang.NullPointerException 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at co.uk.sanetech.wywh.WYWHApplication.getBasicList(WYWHApplication.java:36) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at co.uk.sanetech.wywh.TripsScreenActivity.onResume(TripsScreenActivity.java:42) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at android.app.Activity.performResume(Activity.java:3823) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118) 
10-14 20:41:06.993: ERROR/AndroidRuntime(305):  ... 12 more 

代碼fetchList:

public Cursor fetchList(String tabName) { 

    Log.i(TAG, "In fetchList..."); 

...因爲我沒有得到這最後的調試消息,這表明,(我)這個問題是調用fetchList但不知道爲什麼。

感激地接受任何幫助......

回答

0

從您發佈有代碼,實例dbUtils永遠不會初始化任何東西,所以它是空的。因此,當您嘗試呼叫fetchList()時,您正在調用不存在的實例的方法。

你的聲明或許應該是這個樣子

// Link to DB class 
private DatabaseUtils dbUtils = new DatabaseUtils(); 

// Accessor method - how other modules get access to DB. 
public DatabaseUtils getDBData() { 
    return dbUtils; 
} 

如果您嘗試訪問它的方法之前的對象實例化。

HTH。

+0

非常好 - 這就是訣竅 - 非常感謝(接下來的錯誤!)... – Nelmo

+0

您的歡迎。如果我的回答對您有幫助,請隨時接受它,謝謝。 – Devunwired

0

看看這個:

this.getDBData().fetchList("TRIP_TABLE"); 
  • 或者是null = IMPOSIBLE
  • 或getDBData()的返回null = 更多鈔票
  • 或fetchList(「TRIP_TABLE」);被returnning空(也許TRIP_TABLE犯規EXIS)= 最更多鈔票
+0

哇,謝謝你的快速回復...我的數據庫是空的,其實是應該創建它的onCreate似乎並沒有被稱爲: '代碼 類DbHelper擴展SQLiteOpenHelper { //構造 公共DbHelper(上下文的背景下){ 超(上下文,DB_NAME,null,DB_VERSION); } @Override public void onCreate(SQLiteDatabase db){ Log.i(TAG,「Creating DB ...」); String sql =「create table」+ TRIP_TABLE +「(」+「+ ' – Nelmo