使用RecyclerView在CardView中实现WebView

时间:2017-09-01 10:46:45

标签: android webview nullpointerexception android-recyclerview android-cardview

我在 CardView 中使用 WebView Recyclerview
我正确地实现了它们,但问题是当我运行应用程序时它会崩溃并显示 nullpointer 异常,

日志崩溃报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{andro.petrochemical/andro.petrochemical.webViewNews}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
                                                                              at andro.petrochemical.webViewNews.onCreate(webViewNews.java:33)
                                                                              at android.app.Activity.performCreate(Activity.java:6662)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

WebView CardView xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView_news"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

RecyclerView xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        android:id="@+id/resycleWeb"/>

</LinearLayout>

WebView java类文件:

public class webViewNews extends AppCompatActivity {

    private WebView webviewthis;
    private RecyclerView webVieRes;
    private DatabaseReference mdataRef;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_page);
        webVieRes = (RecyclerView) findViewById(R.id.resycleWeb);
        mdataRef = FirebaseDatabase.getInstance().getReference().child("weber");
        webviewthis = (WebView)findViewById(R.id.webView_news);

            webviewthis.setWebViewClient(new WebViewClient());
            webviewthis.getSettings().setJavaScriptEnabled(true);
            webviewthis.getSettings().setLoadsImagesAutomatically(true);
        }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<post2web,post2webViewHolder> firebaseRecyclerAdapte = new FirebaseRecyclerAdapter<post2web,post2webViewHolder>(

                post2web.class,
                R.layout.web_card,
                post2webViewHolder.class,
                mdataRef
        ) {
            @Override
            protected void populateViewHolder(post2webViewHolder viewHolder, post2web model, int position) {
                viewHolder.setWebViewPost(model.getWebViewPost());
            }
        };
        webVieRes.setAdapter(firebaseRecyclerAdapte);
    }

    public static class post2webViewHolder extends RecyclerView.ViewHolder {

        View mVie;

        public post2webViewHolder(View itemView) {
            super(itemView);
            mVie = itemView;
        }

        public void setWebViewPost(String webViewPost) {
            WebView post_web = (WebView) mVie.findViewById(R.id.webView_news);
            post_web.loadUrl(webViewPost);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要从post2webViewHolder中找到WebView

它不是活动的一部分,因此它是空的

删除webviewthis

答案 1 :(得分:1)

onCreate()

删除webview初始化
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_page);
        webVieRes = (RecyclerView) findViewById(R.id.resycleWeb);
        mdataRef = FirebaseDatabase.getInstance().getReference().child("weber");

    }

并在post2webViewHolder中更新

 public void setWebViewPost(String webViewPost) {
                WebView post_web = (WebView) mVie.findViewById(R.id.webView_news);
                post_web.loadUrl(webViewPost);
                post_web.setWebViewClient(new WebViewClient());
                post_web.getSettings().setJavaScriptEnabled(true);
                post_web.getSettings().setLoadsImagesAutomatically(true);
 }