使用Firebase在Android中打开与网页相关的网页

时间:2017-08-23 07:52:34

标签: android firebase webview firebase-realtime-database android-webview

我有一个类似下面的活动,我想要的是当我点击文字时打开一个webView ,这篇文章的详细信息会通过网页显示。我已经实现了<用于webPage检索的strong> Firebase数据库但它无法正常运行应用程序会立即崩溃我点击它。我搜索了很多问题,但没有得到任何有用的答案,请帮忙。

  

我正在使用Android Studio和Firebase

活动截图

enter image description here

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("webing");
    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 mView;

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

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

webView 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">

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

WebView CardView布局

<?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">

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

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

崩溃日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{andromeda.petrochemical/andromeda.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 andromeda.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) 

1 个答案:

答案 0 :(得分:0)

由于webviewthisnull,这意味着findViewById()必须在以下行中返回null -

webviewthis = (WebView) findViewById(R.id.webView_news);

这是因为您的R.layout.webview_page不包含ID为webView_news的任何视图。

我猜这有助于你。谢谢

注意 -

我不确定您要做什么,但如果您想CardView RecyclerView,请先动态CardView添加RecyclerView,然后再使用 -

webVieRes.findViewById(R.id.webView_news)

即。使用view.findViewById()而不是Activity.findViewById()非常重要。