0

Iam Creating Project for "Drawerwith Swipe Tab " in that i was using a webview in fragment Class , but while Running App its getting getting Crash with a error in log cat, Still getting error

Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient()' on a null object reference

Fragment Class :

  package com.androidbelieve.drawerwithswipetabs;

  import android.graphics.Bitmap;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.ProgressBar;

public class SocialFragment extends Fragment {


ProgressBar pb_per;
WebView mWebView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    pb_per = (ProgressBar) container.findViewById(R.id.progressBar_book1);
    mWebView = (WebView) container.findViewById(R.id.web_book1); //This is the id you gave for webview

    //------------------------------------- to over ride keyboard error ------------(1)
    mWebView.setWebViewClient(new myWebClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    //----------------------------------------------------------------------------


    mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
    //if ROM supports Multi-Touch
    mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
    mWebView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(false);


    // Load URL
    mWebView.loadUrl("http://m.tnstc.in");


    return inflater.inflate(R.layout.social_layout,null);
}

//================================= to over ride keyboard error (2)============================

public class myWebClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
             pb_per.setVisibility(View.VISIBLE);
        //  multi_per.setVisibility(ProgressBar.GONE);

        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
         pb_per.setVisibility(View.GONE);
        //  multi_per.setVisibility(ProgressBar.VISIBLE);

    }
 }
}

XML Code for Fragment Class

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
<WebView
    android:id="@+id/web_book1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="true" />


   <ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar_book1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

 </RelativeLayout>

LogCat Error :

-10 18:03:52.724 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504636): avc: denied { search } for name="31316" dev="proc" ino=3076117 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:system_app:s0 tclass=dir permissive=0
03-10 18:03:52.734 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504637): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0
03-10 18:03:52.734 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504638): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0
03-10 18:03:52.744 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504639): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0
03-10 18:03:52.764 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504640): avc: denied { search } for name="1" dev="proc" ino=6152 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=dir permissive=0
03-10 18:03:52.900 31372-31372/? D/AndroidRuntime: Shutting down VM
03-10 18:03:52.905 31372-31372/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.androidbelieve.drawerwithswipetabs, PID: 31372
                                                   Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
                                                       at com.androidbelieve.drawerwithswipetabs.SocialFragment.onCreateView(SocialFragment.java:30)
                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
                                                       at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
                                                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
                                                       at android.view.View.measure(View.java:18797)
                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                    at android.view.Choreographer.doCall
03-10 18:03:52.976 791-2716/? W/ActivityManager:   Force finishing activity com.androidbelieve.drawerwithswipetabs/.MainActivity
03-10 18:03:52.982 791-1298/? W/SearchableInfo: Invalid searchable metadata for com.bt.bms/.activities.GlobalSearchActivity: Search label must be a resource reference.
03-10 18:03:53.214 791-2716/? W/ActivityManager:   Force finishing activity com.facebook.katana/.activity.ImmersiveActivity
03-10 18:03:53.221 791-1276/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12872 waiters=0 for 305ms
03-10 18:03:53.222 27217-27217/? D/AppStateLogger: Activity activity.ImmersiveActivity changed state to Stopped
03-10 18:03:53.232 27217-27259/? D/AppStateLogger: Successfully dumped app state to log file 
saurabh
  • 13
  • 2
  • 2
  • 11

2 Answers2

0

Modify your code something to this. The layout contains the webview, ProgressBar anything not the container.

package testing.dev.hb.com.testing;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.ProgressBar;

public class SocialFragment extends Fragment {


    ProgressBar pb_per;
    WebView mWebView;
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.social_layout, container, false);

        pb_per = (ProgressBar) view.findViewById(R.id.progressBar_book1);
        mWebView = (WebView) view.findViewById(R.id.web_book1); //This is the id you gave for webview

        //------------------------------------- to over ride keyboard error ------------(1)
        mWebView.setWebViewClient(new myWebClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        //----------------------------------------------------------------------------


        mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
        //if ROM supports Multi-Touch
        mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
        mWebView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(false);


        // Load URL
        mWebView.loadUrl("http://m.tnstc.in");


        return view;
    }

//================================= to over ride keyboard error (2)============================

    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            pb_per.setVisibility(View.VISIBLE);
            //  multi_per.setVisibility(ProgressBar.GONE);

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            pb_per.setVisibility(View.GONE);
            //  multi_per.setVisibility(ProgressBar.VISIBLE);

        }
    }
}
Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
  • Thanks for Helping, I had modify Code but getting new Error " java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference" – saurabh Mar 10 '16 at 13:47
  • can anyone help me out plz – saurabh Mar 10 '16 at 17:36
  • In which class and where? – Nigam Patro Mar 11 '16 at 05:13
  • Its Working for me , Thanks , Can u plz Let me know how can i load url from one class to another fragment class – saurabh Mar 11 '16 at 09:19
  • Its Working for me , Thanks , Can u plz Let me know how can i load url from one class to another fragment class Here is Link , PLZ Help Me : http://stackoverflow.com/questions/35936637/how-can-i-load-webview-url-in-fragment-from-another-appcompatactivity-class – saurabh Mar 11 '16 at 09:39
  • What you can do is, you can define the URL string in Activity class as public and access it by the name it self or you can define a method for that. – Nigam Patro Mar 11 '16 at 09:49
  • @saurabh can you check at the question point, there is a answer posted once try that, if not I will help. – Nigam Patro Mar 11 '16 at 10:26
  • I had Tried Many of EXamples trying it from hour but Total Confused Plz help me yaar thanks a lot – saurabh Mar 11 '16 at 10:38
0

in your onCreateView method you have to invoke findViewById not on container. Inflate a new view and initialize all views from it:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.social_layout, container);
        pb_per = (ProgressBar) view.findViewById(R.id.progressBar_book1);
        mWebView = (WebView) view.findViewById(R.id.web_book1);
        mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                     return false;
                }

                @Override
                public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
                    pb_per.setVisibility(View.VISIBLE);
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public void onPageFinished(final WebView view, final String url) {
                    pb_per.setVisibility(View.GONE);
                    super.onPageFinished(view, url);
                }
            });

        // mWebView settings...

        // Load URL
        mWebView.loadUrl("http://m.tnstc.in");

        return view;
}

And you dont need actually myWebClient class anymore. You can delete it.

yital9
  • 6,544
  • 15
  • 41
  • 54
  • Still Getting Error " java.lang.StackOverflowError: stack size 8MB" – saurabh Mar 11 '16 at 04:56
  • Process: com.androidbelieve.drawerwithswipetabs, PID: 18360 Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} java.lang.StackOverflowError: stack size 8MB – saurabh Mar 11 '16 at 04:58
  • try it without drawerwithswipetabs. The fragment with WebView should work. I suppose, now you have an error somewhere else – yital9 Mar 11 '16 at 06:59