2

我有一个扩展 SherlockFragmentActivity 并实现 ActionBar.TabListener 的活动。

这是 xml 的样子:

<LinearLayout 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"
    android:id="@+id/fragment_layout"
    tools:context=".MyAppActivity" >


    <fragment
        android:id="@+id/tab_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.myapp.android.MyAppWebviewFragment" />        

</LinearLayout>

它在 setContentView 上崩溃,其中 activity_welcome 是上面给出的 xml。

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_welcome);
...

MyAppActivity.java:154 Fragment com.myapp.android.MyAppActivity 中的 java.lang.IllegalStateException 没有创建视图。

Fragment 的 onCreateView 如下所示:

    public class MyAppWebviewFragment extends SherlockFragment{
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        View web = inflater.inflate(R.layout.myapp_webview_fragment, container, false);
        pullToRefreshView = (PullToRefreshWebView) web.findViewById(R.id.tab_content);        

        return web;
    }

      public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        final Activity activity = getActivity();

        if(url != null){
            ...
}

知道为什么会发生这种情况吗?

编辑

堆栈跟踪:

java.lang.IllegalStateException: Fragment com.myapp.android.MyAppWebviewFragment did not create a view.
    FragmentActivity.java:303 • android.support.v4.app.FragmentActivity.onCreateView
    LayoutInflater.java:563 • android.view.LayoutInflater.createViewFromTag
    LayoutInflater.java:623 • android.view.LayoutInflater.rInflate
    LayoutInflater.java:408 • android.view.LayoutInflater.inflate
    LayoutInflater.java:320 • android.view.LayoutInflater.inflate
    LayoutInflater.java:276 • android.view.LayoutInflater.inflate
    ActionBarSherlockCompat.java:840 • com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView
    SherlockFragmentActivity.java:262 • com.actionbarsherlock.app.SherlockFragmentActivity.setContentView
    MyAppActivity.java:154 • com.myapp.android.MyAppActivity.onCreate
    Instrumentation.java:1047 • android.app.Instrumentation.callActivityOnCreate
    ActivityThread.java:1617 • android.app.ActivityThread.performLaunchActivity
    ActivityThread.java:1669 • android.app.ActivityThread.handleLaunchActivity
    ActivityThread.java:117 • android.app.ActivityThread.access$1500
    ActivityThread.java:937 • android.app.ActivityThread$H.handleMessage
    Handler.java:99 • android.os.Handler.dispatchMessage
    Looper.java:130 • android.os.Looper.loop
    ActivityThread.java:3692 • android.app.ActivityThread.main
    Method.java:-2 • java.lang.reflect.Method.invokeNative
    Method.java:507 • java.lang.reflect.Method.invoke
    ZygoteInit.java:842 • com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
    ZygoteInit.java:600 • com.android.internal.os.ZygoteInit.main
    NativeStart.java:-2 • dalvik.system.NativeStart.main
4

0 回答 0