0

I have been trying my best to understand the source material from android studio and the android developer website https://developer.android.com/guide/webapps/webview#java

But I know that a app that just loads a webpage works perfectly for my needs at the moment. So I wanted to diversify. I wanted to see if I could make a webpage that was a bit more dynamic and an app that could use the "back button". What I have so far is in

Android Manifest

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

Activity Main

<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

</android.support.constraint.ConstraintLayout>

and the java main file WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(myWebView);
        myWebView.loadUrl("www.google.com");
        myWebView.getSettings().setJavaScriptEnabled(true);
    }

        private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().equals("www.google.com")) {
                    // This is my website, so do not override; let my WebView load the page
                    return false;
                }
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

My computer cannot handle the android emulator and I try to test the apps on my phone but they keep stopping. I know this may seem super basic to some of you out there, but I am not sure why it keeps malfunctioning. All I want to do is make a simple code that uses the back button, webview and loads other links in webview

I have tried looking for resources elsewhere but they all use the same basic reference material.

  • What does stop/malfunction mean? If a crash, [edit](https://stackoverflow.com/posts/54839757/edit) post with logcat (i.e. follow [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/q/23353173/295004) – Morrison Chang Feb 23 '19 at 08:45

1 Answers1

0

Did you get your myWebView? I guess not!

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

Add this line before myWebView.loadUrl(...);