简体   繁体   中英

Android Studio: How can I load url in WebView on fragment by clicking a button on the same fragment

I read answers in this thread

And I tried it, it worked. But what it did was it opened a new dialog with the url loaded and that's not exactly what I wanted.

I wanted the url to be loaded straight into the WebView element below my button. So basically what I have is a fragment and I have EditText, Button and WebView element all inside of that fragment. What I want to do is click on "search" button and load the url from EditText right into WebView located right below the button and EditBox elements. I don't want it to open my browser or a new dialog.

I have succedeed with automatically loading some url into WebView when you go to that fragment on app, but when I tried to load some url on button click, the app is just crashing. I've tried searching for this issue fix alot before making this thread but nobody seems to have the same problem.

Here's my fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".Main">

    <!-- TODO: Update blank fragment layout -->

    <WebView
        android:id="@+id/webV"
        android:layout_width="match_parent"
        android:layout_height="463dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="48dp" />

    <Button
        android:id="@+id/searchBtn"
        android:layout_width="99dp"
        android:layout_height="43dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/search" />

    <EditText
        android:id="@+id/SVbox"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/search_url"
        android:inputType="text|textPersonName"
        android:autofillHints="" />

</RelativeLayout>

And here in Main.java (the java file for my fragment) I tried this:

package com.something.Something;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

/**
 * A simple {@link Fragment} subclass.
 */
public class Main extends Fragment {



    public Main() {
         //Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        WebView web = (WebView) view.findViewById(R.id.webV);
        web.setWebViewClient(new WebViewClient());
        web.loadUrl("http://randomsite.com");
        Button btn = (Button) view.findViewById(R.id.searchBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText et = (EditText) view.findViewById(R.id.SVbox);
                WebView webz = (WebView) view.findViewById(R.id.webV);
                webz.setWebViewClient(new WebViewClient());
                webz.loadUrl("" + et.getText());
            }
        });
        return view;

    }



}

When I click on button, the app crashes.. I don't know what is the issue even after long research. I'm very new to Android Studio by the way, any help appreciated.

You are making mistake here WebView webz = (WebView) view.findViewById(R.id.webV); In onClick(View view) view is the button that got clicked. So you wouldn't find any Webview and EditText . Inside onClick , instead of initializing webz and et you can make web final and initialize et before btn.setOnClickListener .

Below is the updated code of onCreateView that will work for you.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    final WebView web = (WebView) view.findViewById(R.id.webV);
    web.setWebViewClient(new WebViewClient());
    web.loadUrl("http://randomsite.com");
    final Button btn = (Button) view.findViewById(R.id.searchBtn);
    final EditText et = (EditText) view.findViewById(R.id.SVbox);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            web.loadUrl(et.getText().toString());
        }
    });
    return view;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM