0

我试图在另一个函数中调用一个函数,但我得到一个 nullpointerException 以及许多其他错误..

public float maxFat() {

    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);

    float cals = customSharedPreference.getFloat("calpref", 0f);
    float maxfat = cals / 36;

    return maxfat;
}

这就是我所说的

public XYMultipleSeriesDataset getFatDataset() {

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    Cursor c = database.rawQuery("select * from dailystats;", null);
    float i = 1.0f;

    c.moveToFirst();
    XYSeries seriesFat = new XYSeries("Fat");
    XYSeries seriesMaxFat= new XYSeries("Maximum Fat");
    while (!c.isAfterLast()) {
        seriesFat.add(i++, c.getDouble(2));
        StatsActivity stats= new StatsActivity();
        seriesMaxFat.add(i, stats.maxFat());
        c.moveToNext();
    }
    dataset.addSeries(seriesFat);
    dataset.addSeries(seriesMaxFat);
    return dataset;
}

这是日志文件

05-05 11:39:22.737: E/AndroidRuntime(31282): java.lang.NullPointerException
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.nutriapp.StatsActivity.maxFat(StatsActivity.java:183)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.nutriapp.DatabaseAdapter.getFatDataset(DatabaseAdapter.java:396)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.nutriapp.FatActivity.getIntent(FatActivity.java:52)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.nutriapp.StatsActivity$1.onClick(StatsActivity.java:127)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.view.View.performClick(View.java:3511)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.view.View$PerformClick.run(View.java:14105)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.os.Handler.handleCallback(Handler.java:605)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.os.Looper.loop(Looper.java:137)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 11:39:22.737: E/AndroidRuntime(31282):    at dalvik.system.NativeStart.main(Native Method)
05-05 11:39:22.937: D/dalvikvm(31282): GC_CONCURRENT freed 135K, 3% free 9633K/9863K, paused 17ms+29ms
05-05 11:39:23.047: E/SQLiteDatabase(31282): close() was never explicitly called on database '/data/data/com.nutriapp/databases/nutriwellness.db' 
05-05 11:39:23.047: E/SQLiteDatabase(31282): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at com.nutriapp.DatabaseAdapter.open(DatabaseAdapter.java:215)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at com.nutriapp.FatActivity.getIntent(FatActivity.java:49)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at com.nutriapp.StatsActivity$1.onClick(StatsActivity.java:127)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.view.View.performClick(View.java:3511)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.view.View$PerformClick.run(View.java:14105)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.os.Handler.handleCallback(Handler.java:605)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.os.Looper.loop(Looper.java:137)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 11:39:23.047: E/SQLiteDatabase(31282):    at dalvik.system.NativeStart.main(Native Method)
05-05 11:39:23.047: E/System(31282): Uncaught exception thrown by finalizer
05-05 11:39:23.098: E/System(31282): java.lang.IllegalStateException: Don't have database lock!
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
05-05 11:39:23.098: E/System(31282):    at android.util.LruCache.trimToSize(LruCache.java:197)
05-05 11:39:23.098: E/System(31282):    at android.util.LruCache.evictAll(LruCache.java:285)
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
05-05 11:39:23.098: E/System(31282):    at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
05-05 11:39:23.098: E/System(31282):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
05-05 11:39:23.098: E/System(31282):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
05-05 11:39:23.098: E/System(31282):    at java.lang.Thread.run(Thread.java:856)
4

2 回答 2

1

类是否StatsActivity扩展Activity?如果是这样,您不能使用new关键字来创建该类的新实例,并且您需要重新考虑您的应用程序设计。

于 2012-05-05T21:11:03.603 回答
0

您没有关闭游标,这就是您获得数据库未关闭异常的原因。做就是了

c.close()

完成光标后,在 XYMultipleSeriesDataset 函数中。

于 2012-05-05T11:21:05.477 回答