-1

I have developed an app which contains fragment View but I was exposed to a problem, the app is crashing. Can you help me in solving the problem?

Error I am getting is:

   java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getWidth()' on a null object reference
        at com.abdeljalilkchih.palestine.Fragment.FragmentOne$1.run(FragmentOne.java:59)
04-28 13:45:51.898 23913-24013/com.abdeljalilkchih.palestine E/chromium: [ERROR:gl_context_virtual.cc(39)] Trying to make virtual context current without decoder.

Can you please tell me how can I solve this error?

FragmentOne.Java

 package com.abdeljalilkchih.palestine.Fragment;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    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 com.abdeljalilkchih.palestine.R;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.AdView;
    import yalantis.com.sidemenu.interfaces.ScreenShotable;

    public class FragmentOne extends Fragment implements ScreenShotable {

        private AdView MyAdView2;
        private View Fragmentone_view;
        private Bitmap bitmap;


        public static FragmentOne newInstance() {
            FragmentOne fragmentOne = new FragmentOne();
            return fragmentOne;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_one, container, false);
       MobileAds.initialize(this.getContext(), "ca-app-pub-9961705383410701~3103812044");
            MyAdView2 = rootView.findViewById(R.id.MyAdView2);
            AdRequest adRequest = new AdRequest.Builder().build();
            MyAdView2.loadAd(adRequest);
            WebView webView;
            webView=(WebView) rootView.findViewById(R.id.Web_View2);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient());


            //ربط الفيو بالصفحات
            webView.loadUrl("file:///android_asset/webfiles/index.html");
            return rootView;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            this.Fragmentone_view = view.findViewById(R.id.container);
        }

        @Override
        public void takeScreenShot() {
            Thread threads = new Thread() {
                @Override
                public void run() {
                    Bitmap bitmap = Bitmap.createBitmap(Fragmentone_view.getWidth(),
                            Fragmentone_view.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    Fragmentone_view.draw(canvas);
                    FragmentOne.this.bitmap = bitmap;
                }
            };

            threads.start();

        }

        @Override
        public Bitmap getBitmap() {
            return bitmap;
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

}

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<io.codetail.widget.RevealFrameLayout
    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">

    <FrameLayout
        android:id="@+id/Container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/Main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <!--
your code here
        -->

            <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/MyAdView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="0"
                ads:adSize="BANNER"
                ads:adUnitId="@string/ad_unit_banner2"></com.google.android.gms.ads.AdView>

            <WebView
                android:id="@+id/Web_View2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0" />


        </LinearLayout>
    </FrameLayout>
</io.codetail.widget.RevealFrameLayout>
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • 7
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ben May 22 '18 at 10:32
  • You can use **`getView()`** instead of Fragmentone_view – Jyoti JK May 22 '18 at 10:34
  • Think to remove Ads ids before charing your code – E.Abdel May 22 '18 at 10:46

3 Answers3

0

Looks like you have defined the variable Fragmentone_view locally to method onViewCreated()

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.Fragmentone_view = view.findViewById(R.id.container);
    }

To utilize the variable in another method, you need to define it globally.

Richa
  • 36
  • 4
0
private View Fragmentone_view; 

didnt initialize properly thats why you getting null pointer exception

try below code

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_one,container,false);
        this.Fragmentone_view = rootView
        MobileAds.initialize(this.getContext(), "ca-app-pub9961705383410701~3103812044");
        MyAdView2 = rootView.findViewById(R.id.MyAdView2);
        AdRequest adRequest = new AdRequest.Builder().build();
        MyAdView2.loadAd(adRequest);
        WebView webView;
        webView=(WebView) rootView.findViewById(R.id.Web_View2);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());


        //ربط الفيو بالصفحات
        webView.loadUrl("file:///android_asset/webfiles/index.html");
        return rootView;
    }
Sam Raju
  • 189
  • 8
0

Try this \

replace :

     public class FragmentOne extends Fragment implements ScreenShotable {

         ...
    //replace this line with >> private FrameLayout Fragmentone_view;
        private View Fragmentone_view;
...
}

with :

private FrameLayout Fragmentone_view;