从另一个类调用数据库函数时出现异常

时间:2014-03-03 08:26:14

标签: android

public class DatabaseManipulation extends Activity {
    public void updatingBookdetails(String fileName, Uri address) {
        String strAddress = address.toString();
        System.out.println(fileName + "  filename in updation"); // this prints
        System.out.println(strAddress + "  address in updation"); // this prints

        SQLiteDatabase db;
        db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); // error occurs here
        try {
            System.out.println("BLOW FILE111");
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            db.setVersion(1);






            System.out.println("BLOW FILE222");

            ContentValues val = new ContentValues();
            val.put("bookpath", fileName);
            val.put("lastchapter", strAddress);
            db.update("Bookdetails", val, "bookpath=" + fileName, null);
            System.out.println("BLOW FILE333");
        } catch (Exception e) {
            System.out.println(e + "  errors happens");
        } finally {
            db.close();
        }
    }
}

我从另一个扩展Web Activity的活动中调用它。 文件名和Uri地址从该活动传递说X.classDatabaseManipulation.class 请帮忙

logcat的

03-03 14:20:50.294: E/AndroidRuntime(9955): FATAL EXCEPTION: main
    03-03 14:20:50.294: E/AndroidRuntime(9955): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.dteviot.epubviewer/com.dteviot.epubviewer.MainActivity}: java.lang.NullPointerException
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread.access$1100(ActivityThread.java:123)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.os.Looper.loop(Looper.java:137)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at dalvik.system.NativeStart.main(Native Method)
    03-03 14:20:50.294: E/AndroidRuntime(9955): Caused by: java.lang.NullPointerException
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.dteviot.epubviewer.DatabaseManipulation.updatingBookdetails(DatabaseManipulation.java:25)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.dteviot.epubviewer.EpubWebView.loadChapter(EpubWebView.java:196)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.dteviot.epubviewer.MainActivity.loadEpub(MainActivity.java:257)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.dteviot.epubviewer.MainActivity.onListEpubResult(MainActivity.java:241)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at com.dteviot.epubviewer.MainActivity.onActivityResult(MainActivity.java:220)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.Activity.dispatchActivityResult(Activity.java:4649)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
    03-03 14:20:50.294: E/AndroidRuntime(9955):     ... 11 more

3 个答案:

答案 0 :(得分:1)

您的DatabaseManipulation课程是Activity,但事实并非如此。你不能以Context这种方式使用它。

删除extends Activity。传递有效Context(例如this中的Activity)作为updatingBookdetails()的参数,并在参数上调用openOrCreateDatabase()

答案 1 :(得分:0)

使函数保持静态并在需要的地方进行更改 然后像这个DatabaseManipulation一样从类中调用函数。 updatesBookdetails(fileName,address);

答案 2 :(得分:0)

使用以下代码。

替换下面的行

db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

db = openOrCreateDatabase("epub.db", YourActivity.this.MODE_PRIVATE,null);