1

I'm experiencing an issue with WebViews within my small app. I have the relevant webview with the correct information in my layouts and I have sample code directly from the Android Developer Site that doesn't work for me.

The error is around the: findViewById(R.id.webview)method.

The class with event Listener:

package com.bignerdranch.android.transitionexample;

import android.app.Fragment;
import android.os.Bundle;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import com.bignerdranch.android.transitionexample.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class TransitionFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_transition_scene_1, container, false);
        final Scene scene = Scene.getSceneForLayout(container, R.layout.fragment_transition_scene_2, getActivity());
        Button goButton = (Button)rootView.findViewById(R.id.goButton);
        goButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransitionManager.go(scene);
                WebView myWebView = (WebView) findViewById(R.id.webview);
                myWebView.loadUrl("http://www.example.com");
                Log.d("","Loading Finished");
            }
        });
        return rootView;
    }
}

The Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scene"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    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="com.bignerdranch.android.transitionexample.TransitionFragment">

    <TextView
        android:id="@+id/textView"
        android:text="@string/hello_world"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="@string/hello_world"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="@android:dimen/thumbnail_height"
        android:layout_weight="1.43" />

</LinearLayout>
dazziola
  • 193
  • 1
  • 3
  • 14

1 Answers1

2
 WebView myWebView = (WebView) rootView.findViewById(R.id.webview);

Change your onCreateView method to:

private WebView webView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_transition_scene_1, container, false);
    final Scene scene = Scene.getSceneForLayout(container, R.layout.fragment_transition_scene_2, getActivity());
    Button goButton = (Button)rootView.findViewById(R.id.goButton);
    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TransitionManager.go(scene);
            webView myWebView = (WebView) rootView.findViewById(R.id.webview);

            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.setWebViewClient(new WebViewClient(){

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    progDailog.show();
                    view.loadUrl(url);

                    return true;
                }
                @Override
                public void onPageFinished(WebView view, final String url) {
                    progDailog.dismiss();
                }
            });

            webView.loadUrl("http://www.example.com");

            Log.d("","Loading Finished");
        }
    });
    return rootView;
}
Max Vitruk
  • 413
  • 3
  • 8
  • Thanks for the response, but no luck it seems. Thought this may have been it before, however it didn't work unfortunately. the specific error is pointing to the loadUrl line specifically. Just to mention, the error now is a java.lang.NullPointerException at the aforementioned line. – dazziola Mar 10 '15 at 22:44
  • http://stackoverflow.com/questions/5320288/displaying-android-asset-files-in-a-webview - similar – Max Vitruk Mar 10 '15 at 22:52
  • Cheers again but doesn't seem to be related to that issue. The code makes sense, however still a NullPointerException now around setWebViewClient. As I mentioned before, it's as if the value wv is not being set properly as a WebView object. – dazziola Mar 10 '15 at 23:00
  • Did you added the internet permission in your manifest file ? – Max Vitruk Mar 10 '15 at 23:05
  • I did indeed. The source starts here on Github if you've got a moment. https://github.com/dazziola/rlx-checkout-android/tree/master/app/src/main appreciate the help! It's wrecking my head. – dazziola Mar 10 '15 at 23:10
  • Check my edit. Sorry dude but i must go to sleep, 01:16 here ) I hope it help you little bit. – Max Vitruk Mar 10 '15 at 23:18