java - 如何将网页制作成Android应用程序

标签 java android xml

嗨,我正在尝试制作一个基于网络的应用程序,但我是一个初学者,不能 100% 确定我现在在做什么,请指导我,没有错误,但它说应用程序在以下情况下没有响应我运行它

<小时/>

主要Java

<小时/>
    package com.theverge.www.theverge;

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

    import static com.theverge.www.theverge.R.id.*;


    public class TheVerge extends Activity {

        private WebView the_verge;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_the_verge);

             the_verge = (WebView) findViewById(activity_the_verge_webview);
            // Enable Javascript
            WebSettings webSettings = the_verge.getSettings();
            webSettings.setJavaScriptEnabled(true);
            // Force links and redirects to open in the WebView instead of in a browser
            the_verge.setWebViewClient(new WebViewClient());
            the_verge.loadUrl("http://www.theverge.com");
        }


        @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_the_verge, 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 == action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
<小时/>

这是主要 Activity 的 xml

<小时/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
    android:id="@+id/activity_the_verge_webview"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TheVerge">

    <TextView android:text="" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<小时/>

AndroidManfest xml

<小时/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.theverge.www.theverge" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".TheVerge"
            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>

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

</manifest>

最佳答案

您需要将 WebView 添加到布局文件中。将其更改为类似这样的内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:id="@+id/activity_the_verge_webview"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TheVerge">

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

然后将您的 onCreate() 方法替换为这个方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_verge);

         the_verge = (WebView) findViewById(webView1);
        // Enable Javascript
        WebSettings webSettings = the_verge.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // Force links and redirects to open in the WebView instead of in a browser
        the_verge.setWebViewClient(new WebViewClient());
        the_verge.loadUrl("http://www.theverge.com");
    }

关于java - 如何将网页制作成Android应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955397/

相关文章:

python xml pretty-print 不起作用

java - XStream、循环引用异常

java - 网页跟踪器的数据库设计

java - 我的应用程序中要使用什么 Java XML API - StAX 还是 DOM?

Java 自然语言解析器

android - sqlite3_open_v2 ("/data/data/com.android.packagename/databases/dump.sqlite", &handle, 1, NULL) 失败

java - 更改 GoogleMap 标记 android 的自定义信息窗口的位置

java - 检查类名不应包含特定文本

android - 如何在android中使用GPS_PROVIDER获取当前准确的GPS坐标?

java - 从 xml 格式的响应中读取数据