如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)


問題描述

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

I'm using a WebView in my Android application. My problem is weird because the application throws an exception:

  

07-20 08:33:21.013: ERROR/SQLiteDatabase(728): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file

and I'm not using any database. How to solve this problem?

Source:

Inside the main activity in an onClick:

  String url = listeRubrique.get(rubriqueChoisit).getListeVideo().get(paramInt).getUrl();
    if (url != null)
        {
            Intent intent = new Intent(Main.this,Lecture.class);
            intent.putExtra("url",url);
            startActivity(intent);
        }

The second activity where the WebView is:

public class Lecture extends Activity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lecture);

        webView = (WebView)findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        Intent myIntent = getIntent();
        webView.loadUrl(myIntent.getStringExtra("url"));
    }
}

My first activity:

package com...

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com...

public class Main extends Activity {

//Listes
ArrayList<Video> listeVideo = new ArrayList<Video>();
ArrayList<Chapitre> listeRubrique = new ArrayList<Chapitre>();
//ListViews
private ListView listViewVideo;
private ListView listViewRubrique;
//TextViews
private TextView tvTitreRubrique;
private TextView tvDescriptionRubrique;
private TextView tvTitreVideo;
private TextView tvDescriptionVideo;
private TextView tvDureeVideo;
//Button
private Button btLancer;

int rubriqueChoisit;
int videoChoisit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listViewVideo =(ListView)findViewById(R.id.listContenuRubrique);
    listViewRubrique =(ListView)findViewById(R.id.lvContenu);

    tvTitreRubrique = (TextView)findViewById(R.id.tvTitreRubrique);
    tvDescriptionRubrique = (TextView)findViewById(R.id.tvDescriptionRubrique);
    tvTitreVideo = (TextView)findViewById(R.id.tvTitreVideo);
    tvDescriptionVideo = (TextView)findViewById(R.id.tvDescriptionVideo);
    tvDureeVideo = (TextView)findViewById(R.id.tvDureeVideo);

    btLancer = (Button)findViewById(R.id.btLancer);

    listeVideo.add(new Video(1,"Titre","My Link","Description de la video"));

    listeRubrique.add(new Chapitre(1,"...","...",listeVideo));


    ListViewRubriqueAdapter rubriqueAdapter = new ListViewRubriqueAdapter(Main.this, listeRubrique);
    listViewRubrique.setAdapter(rubriqueAdapter);

    listViewRubrique.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
        {

            ListViewVideoAdapter videoAdapter = new ListViewVideoAdapter(Main.this, listeRubrique.get(paramInt));
            listViewVideo.setAdapter(videoAdapter);

            tvTitreRubrique.setText(listeRubrique.get(paramInt).getTitre());
            tvDescriptionRubrique.setText(listeRubrique.get(paramInt).getDescription());
        }
    });

    listViewVideo.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
        {

            String url = listeRubrique.get(rubriqueChoisit).getListeVideo().get(paramInt).getUrl();
            if (url != null)
            {
                Intent intent = new Intent(Main.this,Lecture.class);
                intent.putExtra("url",url);
                startActivity(intent);
            }
        }
    });
}

參考解法

方法 1:

This problem is caused by an update to your application. Sometimes when upgrading the application the permissions of the cache directories are stuck so that your application cannot write to them. I have found that the permissions for the directories remain under the old PID while the upgraded application is assigned a new PID. Unfortunately, there is no way to update the old installation permissions/PID pairing.

This causes your problem and as far as I can see there is nothing that can fix this except a factory reset of the device.

See http://code.google.com/p/android/issues/detail?id=949 or search for similar exceptions where SQLite cannot open/write or SharedPreferences also experience similar problems.

方法 2:

Seems to me every WebView has its own webview.db per application, and if webview.db was created by another version of the application or the application's signature was different, the current application can't get access the existing webview.db.

I had a similar exception and got rid of it by cleaning all application data and removing the app.

方法 3:

   webView = (WebView)findViewById(R.id.webview);
   webView.getSettings().setJavaScriptEnabled(true);
   Bundle bundle = getIntent().getExtras();       
   webView.loadUrl(bundle.getString("url"));

(by GenmhradekblyabtroiRasel)

參考文件

  1. How to solve error "unable to open database file" when using WebView? (CC BY-SA 3.0/4.0)

#android-webview #Android #Database






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論