填充类片段片段WebView时出错

时间:2013-08-01 23:01:36

标签: android fragment

我想用片段实现webView,.xml就像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<webView
    android:id="@+id/webView01"
    android:layout_width="wrap_content"
    android:layout_height="450dp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <webView
        android:id="@+id/webView02"
        android:layout_width="180dp"
        android:layout_height="50dp"
        />
    <webView
        android:id="@+id/webView03"
        android:layout_width="180dp"
        android:layout_height="50dp"/>

</LinearLayout>
</LinearLayout>

和我处理webview的方式是一样的      public class SelectionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState){
    super.onCreateView(inflater,container,savedInstanceState);
    View view =inflater.inflate(R.layout.selected,container,false);
    return view;    
}
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    WebView webView1=(WebView) getView().findViewById(R.id.webView01);
    String url="#######";
    String url1="####"; String url2="#######";
    webView1.setWebViewClient(new Callback());
    webView1.loadUrl(url);
    WebView webView2=(WebView)getView().findViewById(R.id.webView02);
    WebView webView3=(WebView)getView().findViewById(R.id.webView03);

    webView2.loadUrl(url1);
    webView3.loadUrl(url2);
     }
     private class Callback extends WebViewClient{
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        return(false);
    }

}

当我运行应用程序时,它显示错误

  08-01 18:45:39.426: E/AndroidRuntime(27086): FATAL EXCEPTION: main
  08-01 18:45:39.426: E/AndroidRuntime(27086): java.lang.RuntimeException: Unable to      start activity ComponentInfo{com.rulee.logintested/com.rulee.logintested.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.os.Handler.dispatchMessage(Handler.java:99)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.os.Looper.loop(Looper.java:130)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread.main(ActivityThread.java:3691)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at java.lang.reflect.Method.invokeNative(Native Method)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at java.lang.reflect.Method.invoke(Method.java:507)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at dalvik.system.NativeStart.main(Native Method)
  08-01 18:45:39.426: E/AndroidRuntime(27086): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.Activity.setContentView(Activity.java:1663)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at com.rulee.logintested.MainActivity.onCreate(MainActivity.java:41)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  ... 11 more
  08-01 18:45:39.426: E/AndroidRuntime(27086): Caused by: java.lang.NullPointerException
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at com.rulee.logintested.SelectionFragment.onCreate(SelectionFragment.java:38)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
  08-01 18:45:39.426: E/AndroidRuntime(27086):  ... 20 more

但是当我更改.xml时,将webView更改为简单的TextView,并删除处理webView的代码,修复错误。 任何人都可以给我一些想法。

由于

1 个答案:

答案 0 :(得分:0)

WebView区分大小写,您需要CamelCase。所以将你的第一个w改成W。