問題描述
如何解決使用 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 Gen、mhradek、blyabtroi、Rasel)