问题描述
我是 Android 新手...我使用 Eclipse Juno IDE.
I'm new to Android... I use Eclipse Juno IDE.
我的目标是在打开我的应用程序时将它定向到一个网页.我使用 WebView
概念.已正确授予所有权限(据我所知).Java 代码在 Eclipse 上似乎没有错误.
My aim is to just direct my app to a web page on opening it. I use WebView
concept.Have given all permissions correctly(upto my knowledge). Java Code seems error free on eclipse.
但是,在模拟器上它说net::ERR_CACHE_MISS".在手机上安装该应用程序时,该应用程序说网页不可用"
But, ON EMULATOR IT SAYS "net::ERR_CACHE_MISS".When installed the app on mobile, the APP SAYS "web page not available"
我尝试通过将 url 更改为 www.google.co.in
来测试我的代码,尽管提供了我的首选 url.为此,它也讲述了相同的内容.请帮我解决这些问题...真的会很有帮助...提前致谢...
I tried by changing the url as www.google.co.in
, to test my code, despite giving my preferred url. For that also it tells the same.Kindly help me sort this problems out...It'd really be helpful...Thanks in advance...
下面给出的是我从 AndroidManifest.xml 到 MyActivity.java 的完整代码:
Below given is my complete code from AndroidManifest.xml to 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();
}
}
推荐答案
在我的 MainActivity.java 中进行了一些小的更改后,它终于可以工作了.修改后的代码如下:
Finally it works after some minor changes in my MainActivity.java. Below is the edited code:
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();
}
}
这篇关于ANDROID URL DIRECT 应用程序说“网页不可用",在模拟器上说“net::ERR_CACHE_MISS"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!