2017-07-02 110 views
1

我的應用程序具有接片和一個標籤包含一個片段。片段中有一個卡片視圖。我想當我點擊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新手。你能指導我嗎?

回答

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; 
} 

我希望幫助。