我是 Android 的新手......我使用 Eclipse Juno IDE。
我的目标是将我的应用程序指向打开它的网页。我使用WebView
概念。
已正确授予所有权限(据我所知)。 Eclipse上的Java代码似乎没有错误。
但是,关于EMULATOR IT SAYS" net :: ERR_CACHE_MISS"。 在移动设备上安装应用程序时,APP SAYS"网页不可用"
我尝试将网址更改为www.google.co.in
,以测试我的代码,尽管我提供了我喜欢的网址。对此也是如此。
请帮助我解决这个问题......
它真的很有帮助......
提前谢谢......
下面是我从 AndroidManifest.xml 到 MyActivity.java 的完整代码:
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="21" />
<uses-permission android:name="anroid.permission.INTERNET"/>
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/web_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java:
package com.example.webview;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView Browser;
private class WebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1);
Browser.setWebViewClient(new WebClient());
Browser.loadUrl("www.google.co.in");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(Browser.canGoBack()){
Browser.goBack();
}
else
{
backButtonHandler();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
// Setting Dialog Title
// Setting Dialog Message
alertDialog.setTitle("");
alertDialog.setIcon(R.drawable.dialog_icon);
alertDialog.setMessage("Exit Now?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
答案 0 :(得分:0)
尝试完整地址,例如:
Browser.loadUrl("http://www.google.co.in");
我只知道“www”不起作用。
答案 1 :(得分:0)
最后,在我的 MainActivity.java 中进行一些细微更改后,它才有效。以下是经过编辑的代码:
package com.example.webview;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView Browser;
private class WebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1);
Browser.getSettings().setJavaScriptEnabled(true);
// Give your desired URL below
Browser.loadUrl("http://www.xxxxxxxxxxxxxx.com");
Browser.setWebViewClient(new WebClient());
Browser.setWebChromeClient(new WebChromeClient()});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(Browser.canGoBack())
{
Browser.goBack();
}
else
{
backButtonHandler();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog=new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
// Setting Dialog Message
alertDialog.setTitle("");
alertDialog.setIcon(R.drawable.dialog_icon1);
alertDialog.setMessage("Exit Now?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}