当我点击链接时,webview崩溃

时间:2019-02-19 10:09:34

标签: android android-webview

我正在使用此android应用程序im,它将网站转换为APP。基本上是使用webview到目前为止,我一切正常。唯一的事情是,如果我在应用程序内单击,当我单击网站的链接时,它会崩溃。而且我不知道为什么。如果我按下购买某商品的链接,我会得到一个应用程序已停止错误并崩溃。

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Button bt = (Button)findViewById(R.id.button);


        //init the webview
        webView = (WebView) findViewById(R.id.webView);
        //grab web settings
        WebSettings webSettings= webView.getSettings();
        //get web settings
        webSettings.setJavaScriptEnabled(true);
        //load your url
        webView.loadUrl("https://gvsportscards.com/");
        //for opening links inside app
        webView.setWebViewClient(new WebViewClient());

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.setVisibility(View.VISIBLE);
                webView.loadUrl("https://www.youtube.com/user/GVSportsCards/featured");
            }
        });
    }

    @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 void onBackPressed(){

        if (webView.canGoBack()){

            webView.goBack();


        }else {

            super.onBackPressed();
        }
    }

    @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.play) {

            webView.loadUrl("https://www.youtube.com/channel/UCDOjqC1rwUVd-qQ9ThOTuNA/live");
        }

        return super.onOptionsItemSelected(item);
    }
}

当然,最终目标是能够在应用程序上使用网站的全部功能。快到了!

这里有错误日志

 02-19 02:26:40.009 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 0
02-19 02:26:40.066 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 1
02-19 02:26:43.777 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (23)
02-19 02:26:44.360 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(5)] "YT API ready", source: https://gvsportscards.com/wp-content/plugins/youtube-embed-plus/scripts/ytprefs.min.js?ver=5.0.3 (5)
02-19 02:26:44.604 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(1)] "[object Object]", source: https://gvsportscards.com/wp-content/plugins/paypal-for-woocommerce/assets/js/angelleye-in-context-checkout.min.js?ver=1550572004 (1)
02-19 02:26:44.607 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(45)] "JQMIGRATE: 'hover' pseudo-event is deprecated, use 'mouseenter mouseleave'", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (45)
02-19 02:26:44.608 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(47)] "console.trace", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (47)
02-19 02:26:44.653 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(45)] "JQMIGRATE: jQuery.browser is deprecated", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (45)
02-19 02:26:44.653 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(47)] "console.trace", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (47)
02-19 02:26:44.763 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(45)] "JQMIGRATE: 'ready' event is deprecated", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (45)
02-19 02:26:44.764 7028-7028/com.example.michael.webappgv I/chromium: [INFO:CONSOLE(47)] "console.trace", source: https://gvsportscards.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.4.1 (47)
02-19 02:26:45.503 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 0
02-19 02:26:46.647 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 1
02-19 02:26:47.124 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 0
02-19 02:26:47.197 7028-7028/com.example.michael.webappgv D/ViewRootImpl@4e22ff4[MainActivity]: ViewPostIme pointer 1
02-19 02:26:47.280 7028-7028/com.example.michael.webappgv W/ResourceType: Invalid package identifier when getting bag for resource number 0x0213014a
02-19 02:26:47.367 7028-7028/com.example.michael.webappgv D/ScrollView: initGoToTop
02-19 02:26:47.394 7028-7028/com.example.michael.webappgv D/ScrollView: initGoToTop
02-19 02:26:47.469 7028-7028/com.example.michael.webappgv D/ViewRootImpl@91ddf36[MainActivity]: setView = DecorView@57eae37[] TM=true MM=false
02-19 02:26:47.517 7028-7028/com.example.michael.webappgv D/ViewRootImpl@91ddf36[MainActivity]: dispatchAttachedToWindow
02-19 02:26:47.520 7028-7028/com.example.michael.webappgv W/ResourceType: No package identifier when getting value for resource number 0x00000000
02-19 02:26:47.521 7028-7028/com.example.michael.webappgv D/AndroidRuntime: Shutting down VM
02-19 02:26:47.540 7028-7028/com.example.michael.webappgv E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.michael.webappgv, PID: 7028
                                                                            android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                                at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
                                                                                at android.content.res.Resources.loadXmlResourceParser(Resources.java:2148)
                                                                                at android.content.res.Resources.getLayout(Resources.java:1157)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
                                                                                at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
                                                                                at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
                                                                                at org.chromium.content.browser.input.SelectPopupAdapter.getView(SelectPopupAdapter.java:13)
                                                                                at android.widget.AbsListView.obtainView(AbsListView.java:3189)
                                                                                at android.widget.ListView.measureHeightOfChildren(ListView.java:1448)
                                                                                at android.widget.ListView.onMeasure(ListView.java:1355)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at com.android.internal.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:144)
                                                                                at com.android.internal.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:69)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at com.android.internal.policy.DecorView.onMeasure(DecorView.java:899)
                                                                                at android.view.View.measure(View.java:23297)
                                                                                at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2881)
                                                                                at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1884)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2166)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1780)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7827)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:658)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                                at android.os.Handler.handleCallback(Handler.java:789)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6944)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

活动主体

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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:fitsSystemWindows="true"
    tools:context="com.example.michael.webappgv.MainActivity">
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#3d3a3a"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/button">

    </WebView>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YouTube"
        android:id="@+id/button"
        android:background="#050505"
        android:textColor="#c70808"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用自定义webviewclient

public class LoginWebClient extends WebViewClient {


  @Override
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    super.onReceivedError(view, errorCode, description, failingUrl);
  }

  @Override
  public void onReceivedHttpError(WebView view, WebResourceRequest request,
      WebResourceResponse errorResponse) {
    super.onReceivedHttpError(view, request, errorResponse);
  }
} 

主要记录并向我们显示