Android 在扩展webview的活动中访问数据库时显示错误
/*所有进口商品*/ 公共抽象类EpubWebView扩展了WebView{ 私人最终静态浮动抛投阈值速度=200Android 在扩展webview的活动中访问数据库时显示错误,android,Android,/*所有进口商品*/ 公共抽象类EpubWebView扩展了WebView{ 私人最终静态浮动抛投阈值速度=200 package com.pack.epubreader; /* *图书视图将显示 */ 公共静态数据库sqlitedb; DatabaseManipulation dbManipulation=新建DatabaseManipulation(); //MainActivity lastChapterAddress=新的MainActivity(); mysqlitehelperdb
package com.pack.epubreader;
/*
*图书视图将显示
*/
公共静态数据库sqlitedb;
DatabaseManipulation dbManipulation=新建DatabaseManipulation();
//MainActivity lastChapterAddress=新的MainActivity();
mysqlitehelperdbmanager;
私人书籍;
字符串最后一页;
私人手势检测器mGestureDetector;
/*
*我们当前显示的“根”页面
*/
私有Uri mCurrentResourceUri,extraUri,extraUri\u start,extraUri\u lastPos,extraUri\u chapter;
/*
*文件的位置
*/
私有浮动mScrollY=0.0f;
公共字符串sentFilename=null;
/*
*请注意,我们是从书签加载的
*/
私有布尔值mScrollWhenPageLoaded=false;
/*
*讲课文
*/
私密文本到SpeechWrapper mTtsWrapper;
/*
*以文本形式显示页面
*/
私有ArrayList多行文字;
私有WebViewClient mWebViewClient;
/*
*正在通话的当前线路
*/
私有int mTextLine;
/*
*选择初始默认值
*/
专用浮点数MFLingMindDistance=320;
公共EpubWebView(上下文){
这个(上下文,空);
}
公共EpubWebView(上下文、属性集属性){
超级(上下文,attrs);
mGestureDetector=新的手势检测器(上下文,mGestureListener);
WebSettings=getSettings();
settings.setCacheMode(WebSettings.LOAD\u NO\u缓存);
setWebViewClient(mWebViewClient=createWebViewClient());
}
/*
*@return Android版本合适的WebViewClient
*/
抽象受保护的WebViewClient createWebViewClient();
public void testDataURL(AssetManager AssetManager){
setBook(Environment.getExternalStorageDirectory().getPath());
//String resourceName=“content/calibre\u title\u page.html”;
//String resourceName=“content/074349881X_uup_uusplit_0.html”;
//gingerbreadLoadUrl(Book.resourceName2Url(resourceName));
加载章(空);
}
/*
*展出的书
*/
公共无效设置簿(字符串文件名){
//字符串计数=Intent.getExtra
System.out.println(文件名+“要加载的书”);
sentFilename=文件名;
//如果已加载书本,请不要再次加载
如果((mBook==null)| |!mBook.getFileName().equals(fileName)){
mBook=新书(文件名);
}
}
公共图书{
返回mBook;
}
受保护的WebViewClient getWebViewClient(){
返回mWebViewClient;
}
/*
*书中要展示的章节
*/
公共void loadChapter(Uri resourceUri)
{
如果(mBook!=null)
{
//如果未提供章节资源名称,则默认为第一个章节资源名称。
if(resourceUri==null)
{
resourceUri=mBook.firstChapter();
extraUri=resourceUri;
extraUri_start=resourceUri;
字符串extra_uriStart=extraUri.toString();
//字符串extraUri_start_str=resourceUri.toString();
//dbManipulation.updateingbookDetails(sentFilename,extraUri\u start);
//findLastAddress(extraUri\u start,sentFilename);
System.out.println(sentFilename+“将首先加载的图书名称”);
System.out.println(extraUri_start+“要首先加载的书的地址”);
//db=openOrCreateDatabase(“epub.db”,EpubWebView.this.MODE_PRIVATE,null);此处出错
//db=openOrCreateDatabase(“epub.db”,SQLiteDatabase.CREATE_,如果需要,null);//在这里
尝试
{
System.out.println(“吹制文件111”);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(真);
db.setVersion(1);
System.out.println(“BLOW FILE222”);
ContentValues val=新的ContentValues();
val.put(“bookpath”,文件名);
瓦尔·普特(“最后一章”,斯特拉德雷);
db.update(“Bookdetails”,val,“bookpath=“+fileName,null”);
System.out.println(“BLOW FILE333”);
}
捕获(例外e)
{
System.out.println(e+“发生错误”);
}
最后
{
db.close();
}
//updateBook(sentFilename,extra_uriStart);
System.out.println(sentFilename+“要加载的书的名称”);
//(外部启动);
System.out.println(resourceUri+“顶部的实际Uri”);
String resuristr=resourceUri.toString();
System.out.println(resuristr+“从顶部开始的实际字符串”);
}
if(resourceUri!=null)
{
if(extraUri==null)
{
extraUri=resourceUri;
}
字符串extraUristr=extraUri.toString();
//extraUristr=extraUristr.子字符串(extraUristr.lastIndexOf(“/”)+1);
//extraUristr=extraUristr.子字符串(8);
int p=extraUristr.lastIndexOf(“/”);
extraUristr=extraUristr.子串(0,p);
System.out.println(extraUristr+“切割字符串_1”);
String extraUri2=resourceUri.toString();
int q=extraUri2.lastIndexOf(“/”);
extraUri2=extraUri2.子串(q);
extraUri2=extraUristr+extraUri2;
System.out.println(extruri2+“连接的Uri字符串_2”);
Uri myUri=Uri.parse(extraUri2);
System.out.println(myUri+“-->Joined Uri”);
resourceUri=myUri;
System.out.println(resourceUri+“实际字符串”);
mCurrentResourceUri=resourceUri;
//阻止缓存,因为WebSettings.LOAD\u NO\u缓存并不总是有效。
clearCache(假);
LoadUri(resourceUri);
}
}
}
/*
*@return将URI的内容加载到WebView中,
*实现依赖于android版本
*/
受保护的抽象void LoadUri(Uri);
@凌驾
公共布尔onTouchEvent(运动事件){
返回mGestureDetector.onTouchEvent(事件)||
超级的。
/*
* The book view will show
*/
public static SQLiteDatabase db;
DatabaseManipulation dbManipulation = new DatabaseManipulation();
//MainActivity lastChapterAddress= new MainActivity();
MySQLiteHelper dbManager;
private Book mBook;
String lastPage;
private GestureDetector mGestureDetector;
/*
* "root" page we're currently showing
*/
private Uri mCurrentResourceUri, extraUri, extraUri_start, extraUri_lastPos,extraUri_chapter ;
/*
* Position of document
*/
private float mScrollY = 0.0f;
public String sentFilename=null;
/*
* Note that we're loading from a bookmark
*/
private boolean mScrollWhenPageLoaded = false;
/*
* To speak the text
*/
private TextToSpeechWrapper mTtsWrapper;
/*
* The page, as text
*/
private ArrayList<String> mText;
private WebViewClient mWebViewClient;
/*
* Current line being spoken
*/
private int mTextLine;
/*
* Pick an initial default
*/
private float mFlingMinDistance = 320;
public EpubWebView(Context context) {
this(context, null);
}
public EpubWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, mGestureListener);
WebSettings settings = getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
setWebViewClient(mWebViewClient = createWebViewClient());
}
/*
* @ return Android version appropriate WebViewClient
*/
abstract protected WebViewClient createWebViewClient();
public void testDataURL(AssetManager assetManager) {
setBook(Environment.getExternalStorageDirectory().getPath());
// String resourceName = "content/calibre_title_page.html";
// String resourceName = "content/074349881X__p__split_0.html";
// gingerbreadLoadUrl(Book.resourceName2Url(resourceName));
loadChapter(null);
}
/*
* Book to show
*/
public void setBook(String fileName) {
//String Count=Intent.getExtra
System.out.println(fileName+" book going to load");
sentFilename=fileName;
// if book already loaded, don't load again
if ((mBook == null) || !mBook.getFileName().equals(fileName)) {
mBook = new Book(fileName);
}
}
public Book getBook() {
return mBook;
}
protected WebViewClient getWebViewClient() {
return mWebViewClient;
}
/*
* Chapter of book to show
*/
public void loadChapter(Uri resourceUri)
{
if (mBook != null)
{
// if no chapter resourceName supplied, default to first one.
if (resourceUri == null)
{
resourceUri = mBook.firstChapter();
extraUri=resourceUri;
extraUri_start=resourceUri;
String extra_uriStart=extraUri.toString();
//String extraUri_start_str=resourceUri.toString();
//dbManipulation.updatingBookdetails(sentFilename, extraUri_start);
//lastChapterAddress.findLastAddress(extraUri_start, sentFilename);
System.out.println(sentFilename+" name of book going to load first");
System.out.println(extraUri_start+" address of book going to load first");
//db = openOrCreateDatabase("epub.db", EpubWebView.this.MODE_PRIVATE,null); error here
//db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);// and 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();
}
//dbManager.updateBook(sentFilename, extra_uriStart);
System.out.println(sentFilename+" name of book going to load");
//(extraUri_start);
System.out.println(resourceUri+" actual Uri from top");
String resuristr = resourceUri.toString();
System.out.println(resuristr+" actual string from top");
}
if (resourceUri != null)
{
if(extraUri==null)
{
extraUri=resourceUri;
}
String extraUristr=extraUri.toString();
//extraUristr=extraUristr.substring(extraUristr.lastIndexOf("/") + 1);
//extraUristr= extraUristr.substring(8);
int p=extraUristr.lastIndexOf("/");
extraUristr=extraUristr.substring(0,p);
System.out.println(extraUristr+" cutted string_1");
String extraUri2=resourceUri.toString();
int q=extraUri2.lastIndexOf("/");
extraUri2=extraUri2.substring(q);
extraUri2=extraUristr+extraUri2;
System.out.println(extraUri2+" Joined Uri string_2");
Uri myUri = Uri.parse(extraUri2);
System.out.println(myUri+"--> Joined Uri ");
resourceUri= myUri;
System.out.println(resourceUri+"actual string");
mCurrentResourceUri = resourceUri;
// prevent cache, because WebSettings.LOAD_NO_CACHE doesn't always work.
clearCache(false);
LoadUri(resourceUri);
}
}
}
/*
* @ return load contents of URI into WebView,
* implementation is android version dependent
*/
protected abstract void LoadUri(Uri uri);
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event) ||
super.onTouchEvent(event);
}
GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
// if no book, nothing to do
if (mBook == null) {
return false;
}
// also ignore swipes that are vertical, too slow, or too short.
float deltaX = event2.getX() - event1.getX();
float deltaY = event2.getY() - event1.getY();
if ((Math.abs(deltaX) < Math.abs(deltaY))
|| (Math.abs(deltaX) < mFlingMinDistance)
|| (Math.abs(velocityX) < FLING_THRESHOLD_VELOCITY)) {
return false;
} else {
if (deltaX < 0) {
return changeChapter(mBook.nextResource(mCurrentResourceUri));
} else {
return changeChapter(mBook.previousResource(mCurrentResourceUri));
}
}
}
};
private boolean changeChapter(Uri resourceUri) {
if (resourceUri != null) {
loadChapter(resourceUri);
System.out.println(resourceUri+ " next chapter uri");
return true;
} else {
Utility.showToast(getContext(), R.string.end_of_book);
return false;
}
}
/*
* Store current view into bookmark
*/
public Bookmark getBookmark() {
if ((mBook != null) && (mCurrentResourceUri != null)) {
float contentHeight = (float)getContentHeight();
contentHeight = (contentHeight == 0.0f) ? 0.0f : getScrollY() / contentHeight;
return new Bookmark (
mBook.getFileName(),
mCurrentResourceUri,
contentHeight
);
}
return null;
}
public void gotoBookmark(Bookmark bookmark) {
if (!bookmark.isEmpty()) {
mScrollY = bookmark.mScrollY;
// get notify when content height is available, for setting Y scroll position
mScrollWhenPageLoaded = true;
setBook(bookmark.getFileName());
loadChapter(bookmark.getResourceUri());
}
}
public void speak(TextToSpeechWrapper ttsWrapper) {
mTtsWrapper = ttsWrapper;
if ((mBook != null) && (mCurrentResourceUri != null)) {
mText = new ArrayList<String>();
XmlUtil.parseXmlResource(
mBook.fetch(mCurrentResourceUri).getData(),
new XhtmlToText(mText), null);
mTextLine = 0;
mTtsWrapper.setOnUtteranceCompletedListener(mCompletedListener);
if (0 < mText.size()) {
mTtsWrapper.speak(mText.get(0));
}
}
}
/*
* Send next piece of text to Text to speech
*/
private TextToSpeech.OnUtteranceCompletedListener mCompletedListener =
new TextToSpeech.OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
if (mTextLine < mText.size() - 1) {
mTtsWrapper.speak(mText.get(++mTextLine));
}
}
};
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mFlingMinDistance = w / 2;
}
/*
* Called when page is loaded,
* if we're scrolling to a bookmark, we need to set the
* page size listener here. Otherwise it can be called
* for pages other than the one we're interested in
*/
@SuppressWarnings("deprecation")
protected void onPageLoaded() {
if (mScrollWhenPageLoaded) {
setPictureListener(mPictureListener);
mScrollWhenPageLoaded = false;
}
}
/*
* Need to wait until view has figured out how big web page is
* Otherwise, we can't scroll to last position because
* getContentHeight() returns 0.
* At current time, there is no replacement for PictureListener
*/
@SuppressWarnings("deprecation")
private PictureListener mPictureListener = new PictureListener() {
@Override
@Deprecated
public void onNewPicture(WebView view, Picture picture) {
// stop listening
setPictureListener(null);
scrollTo(0, (int)(getContentHeight() * mScrollY));
mScrollY = 0.0f;
}
};
Context ctx;
public EpubWebView(Context context) {
this(context, null);
this.ctx = context;
}
public EpubWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.ctx = context;
mGestureDetector = new GestureDetector(context, mGestureListener);
WebSettings settings = getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
setWebViewClient(mWebViewClient = createWebViewClient());
}
db = ctx.openOrCreateDatabase("epub.db", EpubWebView.this.MODE_PRIVATE,null);
db = ctx.openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);