我有以下代碼,但是當我運行它時,它告訴我「http://www.google.com」不可用,這是完全垃圾。如果我運行的代碼沒有整個HelloWebViewClient
類,它會正常打開谷歌,除了在預安裝的Android瀏覽器。我想在應用程序中打開它。謝謝。在Android中使用WebView
我的代碼:
public class MainActivity extends Activity {
Button btnGo;
Editable guiNumber;
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
webView = (WebView) findViewById(R.id.viewWeb);
webView.setVisibility(View.GONE);
webView.setWebViewClient(new HelloWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
btnGo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
guiNumber = enterGUI.getText();
if (guiNumber.length() == 8) {
guiNumber.replace(4, 8, "");
gui = Integer.parseInt(enterGUI.getText().toString());
System.out.println(gui);
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
webView.setVisibility(View.VISIBLE);
webView.requestFocus();
webView.loadUrl("http://google.com");
}
else {
enterGUI.setError("Your GUI/ILGU number must be 8 digits long");
}
}
});
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
而且清單:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.frostplant.clublink" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15"/>
<uses-permisssion android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
這看起來像一個權限問題,即使你說你給的權限。你可以發佈清單文件只是爲了仔細檢查? – Caner 2012-07-17 16:18:34
清單貼 – 2012-07-17 16:26:59
我認爲它看起來不錯:( – Caner 2012-07-17 16:33:21