我在应用中使用WebView加载网址时遇到问题。在初始屏幕中,我将有两个文本字段和一个按钮。 (所有字段都在res / layouts / main.xml文件中定义)。单击按钮时,将在Activity类中调用一个方法,我将验证文本字段并使用WebView加载URL。当我运行应用程序时,我得到一个我无法修复的异常。如果有人可以帮助我解决这个问题,那就太好了。任何指针都将受到高度赞赏..
我已粘贴源代码和异常跟踪供您参考..
Main.xml文件,
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/loginbutton"
android:layout_width="212dp"
android:layout_height="wrap_content"
android:onClick="logMe"
android:text="Login" />
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView" />
活动类,
public void logMe(View view){
un = (EditText) findViewById(R.id.userName);
pwd = (EditText) findViewById(R.id.password);
Toast.makeText(this,"Hello "+un.getText().toString()+ " Your password is: "+pwd.getText().toString(), Toast.LENGTH_LONG).show();
if(un.getText().toString().equals("admin")){
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onExceededDatabaseQuota(String url, String
databaseIdentifier, long currentQuota, long estimatedSize,
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2); // try to keep quota size as big as possible else database will not get created in HTML 5 app
}});
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
String dbPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//String dbPath ="/data/data/com.android.HTML5DBExample/databases/";
settings.setDatabasePath(dbPath);
settings.setAppCacheEnabled(false);
webView.loadUrl("file:///android_asset/www/newUI.html");
}
以下是LogCat中的异常跟踪,
12-03 15:47:04.367: E/AndroidRuntime(666): FATAL EXCEPTION: main
12-03 15:47:04.367: E/AndroidRuntime(666): java.lang.IllegalStateException: Could not execute method of the activity
12-03 15:47:04.367: E/AndroidRuntime(666): at android.view.View$1.onClick(View.java:2683)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.view.View.performClick(View.java:3110)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.view.View$PerformClick.run(View.java:11934)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.os.Handler.handleCallback(Handler.java:587)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.os.Looper.loop(Looper.java:132)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.app.ActivityThread.main(ActivityThread.java:4123)
12-03 15:47:04.367: E/AndroidRuntime(666): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 15:47:04.367: E/AndroidRuntime(666): at java.lang.reflect.Method.invoke(Method.java:491)
12-03 15:47:04.367: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-03 15:47:04.367: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-03 15:47:04.367: E/AndroidRuntime(666): at dalvik.system.NativeStart.main(Native Method)
12-03 15:47:04.367: E/AndroidRuntime(666): Caused by: java.lang.reflect.InvocationTargetException
12-03 15:47:04.367: E/AndroidRuntime(666): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 15:47:04.367: E/AndroidRuntime(666): at java.lang.reflect.Method.invoke(Method.java:491)
12-03 15:47:04.367: E/AndroidRuntime(666): at android.view.View$1.onClick(View.java:2678)
12-03 15:47:04.367: E/AndroidRuntime(666): ... 11 more
12-03 15:47:04.367: E/AndroidRuntime(666): Caused by: java.lang.NullPointerException
12-03 15:47:04.367: E/AndroidRuntime(666): at com.android.httpurlproject.HttpURLProjectActivity.logMe(HttpURLProjectActivity.java:49)
12-03 15:47:04.367: E/AndroidRuntime(666): ... 14 more