本文介绍了从anothe类调用数据库功能异常时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
公共类DatabaseManipulation延伸活动{
公共无效updatingBookdetails(字符串文件名,URI地址){
串strAddress = address.toString();
的System.out.println(文件名+的文件名中选择更新); //这个打印
的System.out.println(strAddress +,在地址选择更新); //这个打印 SQLiteDatabase分贝;
DB = openOrCreateDatabase(epub.db,SQLiteDatabase.CREATE_IF_NECESSARY,NULL); //此处出现错误
尝试{
的System.out.println(BLOW FILE111);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(真);
db.setVersion(1);
的System.out.println(BLOW FILE222); ContentValues VAL =新ContentValues();
val.put(bookpath,文件名);
val.put(lastchapter,strAddress);
db.update(Bookdetails瓦尔bookpath =+文件名,NULL);
的System.out.println(BLOW FILE333);
}赶上(例外五){
的System.out.println(E +的错误发生);
} {最后
db.close();
}
}
}
我从它扩展的Web活动的另一个活动调用此。
文件名和乌里
地址是从该活动通过说 X.class
到 DatabaseManipulation。类
请帮助
logcat的
14 03-03:20:50.294:E / AndroidRuntime(9955):致命异常:主要
03-03 14:20:50.294:E / AndroidRuntime(9955):了java.lang.RuntimeException:不提供结果ResultInfo {谁= NULL,请求= 0,结果= -1,数据= {意向(有临时演员)}}到活动{com.dteviot.epubviewer / com.dteviot.epubviewer.MainActivity}:显示java.lang.NullPointerException
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread.access $ 1100(ActivityThread.java:123)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1177)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.os.Handler.dispatchMessage(Handler.java:99)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.os.Looper.loop(Looper.java:137)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread.main(ActivityThread.java:4424)
03-03 14:20:50.294:E / AndroidRuntime(9955):在java.lang.reflect.Method.invokeNative(本机方法)
03-03 14:20:50.294:E / AndroidRuntime(9955):在java.lang.reflect.Method.invoke(Method.java:511)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
03-03 14:20:50.294:E / AndroidRuntime(9955):在dalvik.system.NativeStart.main(本机方法)
03-03 14:20:50.294:E / AndroidRuntime(9955):因:显示java.lang.NullPointerException
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.dteviot.epubviewer.DatabaseManipulation.updatingBookdetails(DatabaseManipulation.java:25)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.dteviot.epubviewer.EpubWebView.loadChapter(EpubWebView.java:196)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.dteviot.epubviewer.MainActivity.loadEpub(MainActivity.java:257)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.dteviot.epubviewer.MainActivity.onListEpubResult(MainActivity.java:241)
03-03 14:20:50.294:E / AndroidRuntime(9955):在com.dteviot.epubviewer.MainActivity.onActivityResult(MainActivity.java:220)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.Activity.dispatchActivityResult(Activity.java:4649)
03-03 14:20:50.294:E / AndroidRuntime(9955):在android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
03-03 14:20:50.294:E / AndroidRuntime(9955):11 ...更多
解决方案
您 DatabaseManipulation
类是一个活动
,但它真的不是。你不能把它作为一个上下文
这样的。
删除延伸活动
。传递一个有效的上下文
(如这个
在活动
)作为参数传递给 updatingBookdetails()
并调用 openOrCreateDatabase()
上的说法。
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();
}
}
}
I am calling this from another activity which extends Web Activity.The file name and Uri
Address are passed from that activity say X.class
to DatabaseManipulation.class
Please help
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
解决方案
Your DatabaseManipulation
class is an Activity
but it really isn't. You cannot use it as a Context
this way.
Remove the extends Activity
. Pass in a valid Context
(such as this
in an Activity
) as an argument to updatingBookdetails()
and call openOrCreateDatabase()
on the argument.
这篇关于从anothe类调用数据库功能异常时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!