【问题标题】:Having problems with Webview from another activity Android Studio?来自另一个活动 Android Studio 的 Webview 有问题?
【发布时间】:2015-02-01 17:40:53
【问题描述】:

我有一个名为 PubliComidas 的项目,在我的项目中有 3 个活动。第一个是SplashScreen,第二个是我的MainActivity,第三个是WebView,它是在MainActivity 中单击按钮时加载的,问题是webview 没有按我的预期工作。

当我运行我的应用程序并单击按钮时,它显示:Web page not avalaible net::ERR_CACHE_MISS,但是它很奇怪,因为我试图在另一个项目中测试相同的代码,PD,当你只在 1 个空白活动中测试它第一次开始一个新项目,相同的步骤,它运行完美,但在这个应用程序中没有,所以这个活动有问题吗?或者 webview 不能显示很多活动,我的第一个和第二个活动工作正常,但第三个没有,我的想法是显示谷歌。我的目标是显示 twitter 提要,但首先如果它不能与 google 一起使用,即使它也不能与 twitter 一起使用。非常感谢您的帮助,谢谢!

这是我的代码

Twitter.java

package com.example.json.publicomidas;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class Twitter extends Activity {

    private WebView mWebView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter);
        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://google.co.cr");
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebViewClient(new MyWebViewClient());

            mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

MyWebViewClient.java

package com.example.json.publicomidas;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        if (Uri.parse(url).getHost().endsWith("www.google.co.cr")){
            return false;

        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        view.loadUrl(url);
        return true;


    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.json.publicomidas" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.json.publicomidas.Twitter"
            android:label="@string/twitter" >
        </activity>
        <activity
            android:name="com.example.json.publicomidas.MainActivity"
            android:label="@string/app_name" >

        </activity>

        <activity
            android:name="com.example.json.publicomidas.Splash_Screen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

        <uses-permission android:name="android.permission.INTERNET" />


    </application>

</manifest>

activity_twitter.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/images"
    tools:context="com.example.json.publicomidas.Twitter">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

【问题讨论】:

    标签: android android-activity webview android-studio


    【解决方案1】:

    权限声明应该在应用标签之外。像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.json.publicomidas" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/logo"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.json.publicomidas.Twitter"
                android:label="@string/twitter" >
            </activity>
            <activity
                android:name="com.example.json.publicomidas.MainActivity"
                android:label="@string/app_name" >
    
            </activity>
    
            <activity
                android:name="com.example.json.publicomidas.Splash_Screen"
                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>
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2017-02-02
      • 1970-01-01
      • 2017-05-21
      • 2013-10-04
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多