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>