Android强制关闭错误

时间:2011-10-14 21:32:56

标签: android

Android和Java的新手(我最初是Pascal程序员)并且遇到问题 - 我的应用程序正在关闭,我无法解决原因。

我只是试图调用一个活动来显示我(当前为空)数据库中的数据表。我正在使用“学习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但不确定原因。

感激不尽的任何帮助......

2 个答案:

答案 0 :(得分:0)

看看这个:

this.getDBData().fetchList("TRIP_TABLE");
  • 或者这是null = imposible
  • 或getDBData()返回null = posible
  • 或fetchList(“TRIP_TABLE”);返回null(也许TRIP_TABLE并不存在)= 最有可能

答案 1 :(得分:0)

从您在那里发布的代码中,实例dbUtils永远不会初始化为任何内容,因此它为null。因此,当您尝试调用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。