如何将网页变成Android应用程序

时间:2015-06-20 15:02:10

标签: java android xml

您好我正在尝试制作基于网络的应用,但我是初学者而不是100%确定我现在正在做什么请您指导我没有错误但它说应用程序在我运行时没有响应

Main Java

function findMatch(arr1, arr2) {
  for (i=0; i < arr1.length; i++) {
    for (j=0; j < arr2.length; j++) {
      if (arr1[i] === arr2[j]) {
        return true
      }
    }
  }
  
  return false
}

这是主要活动的xml

    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);
        }
    }

AndroidManfest 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" />

3 个答案:

答案 0 :(得分:1)

您正尝试将relativelayout设置为webview变量:    the_verge =(WebView)findViewById(activity_the_verge_webview);

  1. 您需要在布局中添加一个网页视图并为其添加ID
  2. 将webview变量设置为您刚刚在1中使用的ID。
  3. 此外,如果您将设备插入计算机,则程序中会出现崩溃日志:)

答案 1 :(得分:1)

您已在xml中定义了textview而不是webview。试试这个......

xml layout

<?xml version="1.0" encoding="utf-8"?>
<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/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout>

爪哇

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_xml_name);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

}

}

答案 2 :(得分:1)

您需要将WebView添加到布局文件中。把它改成这样的东西

public function cleanInput($value)
{
    if (is_array($value))
    {
        $newVal = '';
        foreach($value as $field => $value)
        {
            $newVal[$field] = $this->link->real_escape_string($value);
        }
    }
    else
    {
        $newVal = $this->link->real_escape_string(trim(htmlspecialchars($value)));
    }

    return $newVal;
}


  // In example
  $oldData = 20; // input is integer
  $newData = cleanInput($oldData); // output should still be integer
  if (is_int($newData))
  {
     echo "Working!";
  }
  else
  {
     echo "Not working...";
  }

然后将此<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()