如何解决“无法打开数据库文件”错误当使用WebView时?

发布于 2024-11-25 02:26:46 字数 4192 浏览 0 评论 0原文

我在 Android 应用程序中使用 WebView。我的问题很奇怪,因为应用程序抛出异常:

07-20 08:33:21.013: 错误/SQLiteDatabase(728): android.database.sqlite.SQLiteCantOpenDatabaseException: 无法打开数据库文件

,并且我没有使用任何数据库。如何解决这个问题呢?

来源:

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);
        }

WebView 所在的第二个活动:

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"));
    }
}

我的第一个活动:

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);
            }
        }
    });
}

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);
            }
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-12-02 02:26:46

此问题是由您的应用程序更新引起的。有时,升级应用程序时,缓存目录的权限会被卡住,导致应用程序无法写入它们。我发现目录的权限仍然保留在旧的 PID 下,而升级的应用程序被分配了新的 PID。不幸的是,无法更新旧的安装权限/PID 配对。

这会导致您的问题,据我所知,除了设备恢复出厂设置之外,没有什么可以解决此问题。

请参阅 http://code.google.com/p/android/issues/ detail?id=949 或搜索类似的异常,其中 SQLite 无法打开/写入或 SharedPreferences 也遇到类似的问题。

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.

尛丟丟 2024-12-02 02:26:46

在我看来,每个 WebView 每个应用程序都有自己的 webview.db,如果 webview.db 是由应用程序的另一个版本或应用程序的签名不同,当前应用程序无法访问现有的webview.db

我遇到了类似的异常,并通过清理所有应用程序数据并删除应用程序来摆脱它。

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.

苍白女子 2024-12-02 02:26:46
   webView = (WebView)findViewById(R.id.webview);
   webView.getSettings().setJavaScriptEnabled(true);
   Bundle bundle = getIntent().getExtras();       
   webView.loadUrl(bundle.getString("url"));
   webView = (WebView)findViewById(R.id.webview);
   webView.getSettings().setJavaScriptEnabled(true);
   Bundle bundle = getIntent().getExtras();       
   webView.loadUrl(bundle.getString("url"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文