通过Android Studio创建webview

通过Android Studio创建webview,webview,android-studio,Webview,Android Studio,我创建android应用程序的第一天就被卡住了。我跟随了o。然而,经过无数个小时,我仍然无法创建webview应用程序。应用程序甚至在加载之前就在模拟器中停止,并显示错误:“不幸的是,应用程序已停止” 任何帮助都将不胜感激。我的档案如下: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/

我创建android应用程序的第一天就被卡住了。我跟随了o。然而,经过无数个小时,我仍然无法创建webview应用程序。应用程序甚至在加载之前就在模拟器中停止,并显示错误:“不幸的是,应用程序已停止”

任何帮助都将不胜感激。我的档案如下:

AndroidManifest.xml

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

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

package myApp2.com.example.myapplication2.myapp2;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MainActivity extends ActionBarActivity {
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView.loadUrl("http://beta.html5test.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.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
03-15 13:44:56.652    1995-1995/myApp2.com.example.myapplication2.myapp2 D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-15 13:45:01.042    1995-1995/myApp2.com.example.myapplication2.myapp2 W/ActivityThread﹕ Application myApp2.com.example.myapplication2.myapp2 is waiting for the debugger on port 8100...
03-15 13:45:01.112    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ Sending WAIT chunk
03-15 13:45:01.162    1995-2001/myApp2.com.example.myapplication2.myapp2 I/dalvikvm﹕ Debugger is active
03-15 13:45:01.322    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ Debugger has connected
03-15 13:45:01.332    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:01.572    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:01.772    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:01.982    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:02.202    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:02.522    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:02.762    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:02.992    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:03.212    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:03.402    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:03.622    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:03.832    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:04.062    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:04.292    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:04.532    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:04.742    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:04.992    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:05.192    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ waiting for debugger to settle...
03-15 13:45:05.532    1995-1995/myApp2.com.example.myapplication2.myapp2 I/System.out﹕ debugger has settled (1509)
03-15 13:45:12.782    1995-1995/myApp2.com.example.myapplication2.myapp2 D/AndroidRuntime﹕ Shutting down VM
03-15 13:45:12.862    1995-1995/myApp2.com.example.myapplication2.myapp2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3aa7ba8)
03-15 13:45:14.102    1995-1995/myApp2.com.example.myapplication2.myapp2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: myApp2.com.example.myapplication2.myapp2, PID: 1995
    java.lang.RuntimeException: Unable to start activity ComponentInfo{myApp2.com.example.myapplication2.myapp2/myApp2.com.example.myapplication2.myapp2.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at myApp2.com.example.myapplication2.myapp2.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

嘿,我已经成功了。只需按如下方式重新排列代码 onCreate():-

Android清单文件:- 在


请尝试以下代码:

公共类MainActivity扩展了AppCompatActivity{

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

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

    // If you deal with HTML then execute loadData instead of loadUrl
    //webView.loadData("YOUR HTML CONTENT","text/html", "UTF-8");
}
您必须在布局中设置WebView并使用Id定义它

不要忘记清单中的权限

  • 您将使用Android studio在包
    com.example.sairamkrishna.myapplication
    下创建一个Android应用程序
  • 修改src/MainActivity.java文件以添加
    WebView
    code
  • 修改res/layout/activity_main以添加相应的XML组件
  • 修改AndroidManifest.xml以添加必要的权限
  • 运行应用程序,选择一个正在运行的android设备,在上面安装应用程序,并验证结果

  • 具有所有功能和progressbar的Webview

    fragment_webview.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".NewsFragment">
    
    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_2sdp"
        android:background="@color/white" />
    
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </LinearLayout>
    

    MainActivity.Java

    package com.jtech.webviewapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView webView=(WebView) findViewById(R.id.wb);
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("https://stackoverflow.com/");
    }}
    

    我重新整理了您的代码。只需复制paste onCreate()并在清单文件中重新排序标记。这可能会对您有所帮助
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://google.com");
    
        // If you deal with HTML then execute loadData instead of loadUrl
        //webView.loadData("YOUR HTML CONTENT","text/html", "UTF-8");
    }
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".NewsFragment">
    
    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_2sdp"
        android:background="@color/white" />
    
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </LinearLayout>
    
    WebView mWebView;
    ProgressBar progressBar;
    
    
            mWebView = (WebView) findViewById(R.id.webview);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setMax(100);
    
            // Enable Javascript
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
            mWebView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
    
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(0);
                }
    
                public void onPageFinished(WebView view, String url) {
    
                    progressBar.setVisibility(View.GONE);
                    progressBar.setProgress(100);
                }
    
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    
                    Toast.makeText(getContext(), "Something went wrong!", Toast.LENGTH_SHORT).show();
                    progressBar.setVisibility(View.GONE);
                }
            });
            mWebView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
    
                    progressBar.setProgress(progress); //Make the bar disappear after URL is loaded
                    if (progress == 100) {
                        progressBar.setVisibility(View.GONE);
                    }
                }
            });
            mWebView.loadUrl(webUrl);
    
    package com.jtech.webviewapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView webView=(WebView) findViewById(R.id.wb);
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("https://stackoverflow.com/");
    }}