0

So far no one has been able to provided an answer or solution that resolves this. I hope someone has something to contribute because I'm at a loss. And this article (What is a NullPointerException, and how do I fix it?), even if everyone keeps recommending it as the 'go to guide' for 'null exceptions', I find it hard to apply to my situation as it relates to webView and the settings I've provided in the java script below.

If I remove the webView lines from the activity, the activity's page loads fine in the app. All white and without content of course, but it loads. Once I add the webView code back to the activity; the app crashes as soon as it attempts to load. While it appears to crash, the app actually loads the activity page in all white and throws itself to the device's background without closing and pops up a notification stating the obvious, that the app has crashed. It doesn't really exit the app, it just throws it to the backgrounds and sends up an error message. So I believe that the remedy can be found in reviewing my webView related code, but I can't find it and none of the previously recommended methods remedied it. And I should note that this is happening both on my actual connected device and on Android Studio's emulator, so I don't believe it to be phone settings or a cache issue either.

Here is my LogCat

05-08 14:19:03.423 31797-31797/com.app.sega E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.sega, PID: 31797
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.sega/com.app.sega.sega}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
        at com.app.sega.sega.onCreate(sega.java:18)

I've just updated my files content below in trying to fix this. This is what I have as of right now. Getting the same results.

The webView script on the new activity in sega.java

package com.app.sega;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class sega extends AppCompatActivity {
    private WebView webview_s;

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

        webview_s = (WebView)webview_s.findViewById(R.id.webview_sega);
        webview_s.getSettings().getJavaScriptEnabled();
        webview_s.setWebViewClient(new WebViewClient_s());
        webview_s.setInitialScale(1);
        webview_s.getSettings().getBuiltInZoomControls();
        webview_s.getSettings().getUseWideViewPort();

    }

    private class WebViewClient_s extends WebViewClient {

        public boolean shouldOverrideURLLoading (WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.southeastgeorgiatoday.com")) {
                return false;
            }else {
                Intent intent_sega = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent_sega);
                return true;
            }

        }
    }
}

I see nothing on line (sega.java:18) that is left empty/null. This is what is on line 18:

        webview_s = (WebView)webview_s.findViewById(R.id.webview_sega);

Where is there a null entry? Here is the webView xml located in activity_sega.xml. you can see that I've entered the correct webView id in the java code above.

        <WebView android:id="@+id/webview_sega"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
D.Davis
  • 15
  • 1
  • 10

1 Answers1

0

Well I finally found the formula that did the trick. I removed the 'webview_s.' from this line...

webview_s = (WebView)webview_s.findViewById(R.id.webview_sega);

so that it became...

webview_s = (WebView) findViewById(R.id.webview_sega);

Now my entire java script for the sega activity looks like...

package com.app.sega;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class sega extends AppCompatActivity {
    private WebView webview_s;

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

        webview_s = (WebView) findViewById(R.id.webview_sega);
        webview_s.setWebViewClient(new WebViewClient_s());
        webview_s.setInitialScale(1);
        webview_s.getSettings().getJavaScriptEnabled();
        webview_s.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webview_s.getSettings().getBuiltInZoomControls();
        webview_s.getSettings().getUseWideViewPort();
        webview_s.loadUrl("http://www.southeastgeorgiatoday.com");
    }

    private class WebViewClient_s extends WebViewClient {

        public boolean shouldOverrideURLLoading (WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.southeastgeorgiatoday.com")) {
                return false;
            }else {
                Intent intent_sega = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent_sega);
                return true;
            }
        }
    }
}

my layout xml for sega looks like...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

and the manifest looks like...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.app.sega">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".facebook" android:parentActivityName=".MainActivity" />
        <activity android:name=".twitter" android:parentActivityName=".MainActivity" />
        <activity android:name=".wyum" android:parentActivityName=".MainActivity" />
        <activity android:name=".wtcq" android:parentActivityName=".MainActivity" />
        <activity android:name=".wvop" android:parentActivityName=".MainActivity" />
        <activity android:name=".sega" android:parentActivityName=".MainActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My main activity layout for the sega ImageView button which loads the sega activity when clicked looks like...

<ImageView
        android:id="@+id/s"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:autoLink="web"
        android:contentDescription="@string/southeast_georgia_today_official_site"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:longClickable="true"
        android:onClick="@android:string/copyUrl"
        android:scaleType="fitXY"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/y"
        app:layout_constraintEnd_toStartOf="@+id/v"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/s"
        tools:visibility="visible" />

the java script for that ImageButton on the main activity looks like...

        ImageView img_s = (ImageView) findViewById(R.id.s);
        img_s.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_s = new Intent(MainActivity.this, sega.class);
                startActivity(intent_s);
            }
        });

Now my other webviews are set up the same way and they are showing the webView activities properly but the site's/urls are not rendering in the view. I'm not getting an error because the app is showing the activity, it is just that the webView activity is not displaying the content properly. It is displaying the sega url properly, but none of the others (music streaming links and facebook/twitter walls). A question for another topic.

D.Davis
  • 15
  • 1
  • 10