0

I developed an app from example from WebView load website when online, load local file when offline and it sucessfully installed but now I'm getting error i.e is Splash screen loads and then suddenly it stop with Error "Unfortunately CDPRoutine ( !i.e my app ) is Stopped", I don't know what am I doing wrong ....Any Help would save me from getting insane and would be a much much great help. Here is the code from Mainactivity.Java

package com.example.sagar.cdproutine;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
WebView webView;
Context context;

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


    WebView webView = new WebView( context );
    webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
    webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled( true );
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default



    if ( !isNetworkAvailable() ) { // loading offline
        webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }

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





}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

}

Here is the Androidmanifest file

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

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

<application
android:allowBackup="true"
android:icon="@mipmap/app"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>

</manifest>

Here is Error I'm getting

        E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.sagar.cdproutine, PID: 6624

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sagar.cdproutine/com.example.sagar.cdproutine.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                            at android.view.ViewConfiguration.get(ViewConfiguration.java:364)
                                                                            at android.view.View.<init>(View.java:3788)
                                                                            at android.view.View.<init>(View.java:3892)
                                                                            at android.view.ViewGroup.<init>(ViewGroup.java:573)
                                                                            at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:55)
                                                                            at android.webkit.WebView.<init>(WebView.java:597)
                                                                            at android.webkit.WebView.<init>(WebView.java:542)
                                                                            at android.webkit.WebView.<init>(WebView.java:525)
                                                                            at android.webkit.WebView.<init>(WebView.java:512)
                                                                            at android.webkit.WebView.<init>(WebView.java:502)
                                                                            at com.example.sagar.cdproutine.MainActivity.onCreate(MainActivity.java:26)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • Did you add your Activity to AndroidManifest.xml and defined it as LAUNCHER Activity? – lidkxx Jul 14 '17 at 15:01
  • I'm really sorry but I'm completely unaware of anyof that as I am totally new to programming and this project I build with totally on mentioned link for webview as it was exactly what I wanted for my college project. Here is the same project I have shared on git hub if it could be anyhelp to identify a problem and for solution https://github.com/BlueYeti1881/CDPRoutine11 – Sagar Rawal Jul 14 '17 at 15:17
  • Ok now that I've added AndroidManifest.xml file, can you please check if anything wrong there. – Sagar Rawal Jul 14 '17 at 15:57
  • Ok, look at the name of your Activity. Is it really SplashScreen? Looking at the name of your class, I don't think so. Your program cannot start because it cannot find the right one and you need to either change the name of your class or give the correct name in manifest file. – lidkxx Jul 17 '17 at 08:52

0 Answers0