简体   繁体   English

Android Java 在应用程序中显示网站

[英]Android Java Display website in app

I am trying to have a web page appear in my app.我正在尝试在我的应用程序中显示一个网页。 This is what I have done:这就是我所做的:

In my Manifest I added the following code:在我的清单中,我添加了以下代码:

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

In my content_main.xml I added the following code:在我的 content_main.xml 中,我添加了以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <WebView android:id="@+id/webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

In my main Activity.java file added the following code:在我的主 Activity.java 文件中添加了以下代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String url = "http://bigdaddyapp.com";

        webview = (WebView) findViewById(R.id.myWebView);
        //next line explained below
        webview.setWebViewClient(new MyWebViewClient(this));
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

But I get the following errors:但我收到以下错误:

在此处输入图片说明

What am I doing wrong?我究竟做错了什么?

You have quite a few issues here.你这里有很多问题。

First, you never declare your webView variable.首先,你永远不会声明你的webView变量。 In this case, the fix is simple.在这种情况下,修复很简单。 You just need to define its type inline:你只需要内联定义它的类型:

WebView webview = (WebView) findViewById(R.id.myWebView);

Second, you gave your WebView the ID "webView" in your layout XML, but you try to refer to it via R.id.myWebView .其次,您在布局 XML 中为 WebView 指定了 ID“webView”,但您尝试通过R.id.myWebView引用它。 Switch this to use the same ID and it will work.将此切换为使用相同的 ID,它将起作用。

For the third error, it sounds like you don't actually have a class called MyWebViewClient .对于第三个错误,听起来您实际上没有名为MyWebViewClient的类。 Make sure that you do and that it is public.确保你这样做并且它是公开的。

You also aren't setting the correct layout.您也没有设置正确的布局。 You are calling setContentView(R.layout.activity_main) , but your layout is called "content_main."您正在调用setContentView(R.layout.activity_main) ,但您的布局称为“content_main”。

The rest of the errors should disappear once you fix these issues.解决这些问题后,其余错误应该会消失。

setContentView(R.layout.activity_main);

应该:

setContentView(R.layout.content_main);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM