尝试调用虚方法' int android.webkit.WebView.loadUrl()'在null对象引用上

时间:2015-08-10 15:16:06

标签: java android android-fragments webview

我一直在使用相同的方法来扩充我所有其他片段中的所有其他小部件,但由于某些原因它不在这里工作。

ResultFragment.java:

public class ResultFragment extends Fragment {

  private WebView webPageResult;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_question, parent, false);

    webPageResult = (WebView) v.findViewById(R.id.webPageResult);
    webPageResult.loadUrl("google.com");

    return v;
  }

}

fragment_result.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.danielbalagula.gift.ResultFragment">

  <WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webPageResult"   
    android:layout_gravity="center_horizontal|bottom"/>

</FrameLayout>

1 个答案:

答案 0 :(得分:2)

您正在夸大错误的布局,尝试让fragment_result而不是fragment_question充气。

使用View v = inflater.inflate(R.layout.fragment_result, parent, false);