我的应用程序有标签,标签包含一个片段。片段中有一个cardview。我想当我点击cardview时,我想让webview像this一样工作,但我收到了一个错误。 我的webview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/mywebview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我的数据适配器
public static class ContentViewHolder extends RecyclerView.ViewHolder {
protected TextView title;
protected TextView description;
protected TextView date;
protected ImageView imgSrc;
protected String urlS;
protected int itemPos;
WebView mWebView;
AdvancedWebView vw;
public ContentViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
description = (TextView) v.findViewById(R.id.descript);
date = (TextView) v.findViewById(R.id.date);
imgSrc=(ImageView)v.findViewById(R.id.thumbnail);
v.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
/*Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlS));
v.getContext().startActivity(myIntent);*/
//there is a problem
mWebView =(WebView)v.findViewById(R.id.mywebview);
mWebView.loadUrl("http://www.google.com");
}
});
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.oguz.topluluk, PID: 3449
java.lang.NullPointerException
at com.example.oguz.topluluk.WebDataAdapter$ContentViewHolder$1.onClick(WebDataAdapter.java:113)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我该如何解决?我是android的新手。你能指导我吗?
答案 0 :(得分:1)
我解决了这个问题。我看到我没有启动webview也许可能有另一个原因,因为这个原因,我得到了错误。经过一些更改后,代码的最终状态就像那样;
@Override
public void onBindViewHolder(final ContentViewHolder contentViewHolder, int i) {
contentViewHolder.theCard.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
/*...*/
WebView mWebView =new WebView(context);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(wb.link);
}
});
}
在另一个函数中定义上下文,
private Context context;
private List<WebDataInfo> dataList;
public WebDataAdapter(Context context, List<WebDataInfo> dataList) {
this.context=context;
this.dataList = dataList;
}
我希望有所帮助。