java - Java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getWidth()”
问题描述
我开发了一个包含片段视图的应用程序,但我遇到了一个问题,应用程序崩溃了。你能帮我解决问题吗?
我得到的错误是:
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.
你能告诉我如何解决这个错误吗?
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>
解决方案
尝试这个 \
代替 :
public class FragmentOne extends Fragment implements ScreenShotable {
...
//replace this line with >> private FrameLayout Fragmentone_view;
private View Fragmentone_view;
...
}
和 :
private FrameLayout Fragmentone_view;
推荐阅读
- oracle-apex - 在 Oracle APEX 5.1.4 中显示大型 CLOBS
- android - 使用部署和上传证书签署颤振 apk
- c++ - 我可以使用字符串变量而不是名称创建容器吗?C++
- python-3.x - Keras Init Sequential 模型层按模型层
- python - 如何在 django 中自定义 {% bootstrap_form form %}?
- javascript - 如何在 React Native 中切换状态
- opentracing - Jaeger 服务未显示在 Jaeger UI 中
- scala - 为什么在 Akka Streams 中添加异步边界会消耗大量 CPU?
- python - Eclipse PyDev 忽略 __init__,导致未解决的模块导入错误
- laravel - @Laravel 迁移:无法在 laravel 中添加外键约束