当我用一个带有 webview 的片段添加选项卡布局时,应用程序停止了
Posted
技术标签:
【中文标题】当我用一个带有 webview 的片段添加选项卡布局时,应用程序停止了【英文标题】:Application getting stopped when i added tab layout with one fragment with webview 【发布时间】:2018-02-20 05:46:54 【问题描述】:LOGCAT 错误报告
02-20 10:46:13.344 7295-7295/seacoders.abhilash.bogguru E/dalvikvm: Could not find class 'android.support.v4.widget.DrawerLayout$1', referenced from method android.support.v4.widget.DrawerLayout.<init>
02-20 10:46:13.352 7295-7295/seacoders.abhilash.bogguru E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onDraw
02-20 10:46:13.354 7295-7295/seacoders.abhilash.bogguru E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
02-20 10:46:13.356 7295-7295/seacoders.abhilash.bogguru E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
02-20 10:46:13.424 7295-7295/seacoders.abhilash.bogguru E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-20 10:46:14.055 7295-7295/seacoders.abhilash.bogguru E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at seacoders.abhilash.bogguru.HomeFragment$MyWebViewClient.onPageStarted(HomeFragment.java:55)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:331)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
FRAGMENTHOME.java
package seacoders.abhilash.bogguru;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
public class HomeFragment extends Fragment
private WebView wv;
private TextView txt;
ProgressBar pbar;
public static final String TITLE = "Home";
public static HomeFragment newInstance()
return new HomeFragment();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
//return inflater.inflate(R.layout.fragment_home, container, false);
View v=inflater.inflate(R.layout.fragment_home, container, false);
wv = (WebView)v.findViewById(R.id.webview);
wv.loadUrl("http://www.google.com");
WebSettings webSettings = wv.getSettings();
webSettings.setjavascriptEnabled(true);
pbar = (ProgressBar) container.findViewById(R.id.pg1);
txt = (TextView) container.findViewById(R.id.txtload);
wv.setWebViewClient(new MyWebViewClient());
return v;
public class MyWebViewClient extends WebViewClient
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
wv.loadUrl("file:///android_asset/error.html");
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
super.onPageStarted(view, url, favicon);
System.out.println("loading... please wait");
pbar.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
@Override
public void onPageFinished(WebView view, String url)
System.out.println("finished loading");
pbar.setVisibility(View.GONE);
txt.setVisibility(View.GONE);
【问题讨论】:
What is a NullPointerException, and how do I fix it?的可能重复 也显示你的 xml .. 第 55 行的内容。可能pbar
或 txt
是 null 。将视图 ID 与布局 xml 匹配。
pbar & txt 不是 null 已经在 fragment_home.xml 中声明 下面几行的问题
pbar = (ProgressBar) container.findViewById(R.id.pg1);
txt = (TextView) container.findViewById(R.id.txtload);
你在container
上被称为findViewById
方法,你应该调用v
而不是container
。
解决方案
pbar = (ProgressBar) v.findViewById(R.id.pg1);
txt = (TextView) v.findViewById(R.id.txtload);
谢谢
【讨论】:
非常感谢以上是关于当我用一个带有 webview 的片段添加选项卡布局时,应用程序停止了的主要内容,如果未能解决你的问题,请参考以下文章