0

对不起,因为我问了老问题。但我尝试了所有样本,但都没有工作。
应用程序第二次创建视图时抛出异常

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = LayoutInflater.from(getActivity()).inflate(getContentView(), null, false);
    onInitializeView();
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (view != null) {
        ViewGroup parentViewGroup = (ViewGroup) view.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViews();
        }
    }
}

请帮我解决这里是onInitializeView:

protected void onInitializeView() {
    super.onInitializeView();
    // header text
    TextView tvHeader = (TextView) findViewById(R.id.tvHeader);
    tvHeader.setSelected(true);
    tvHeader.setText(DataLocator.INSTANT.getCurrentCategory().getTitle());
    // set title
    TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
    tvTitle.setText(article.getTitle());
    TextView tvDatetime = (TextView) findViewById(R.id.tvDatetime);
    tvDatetime.setText(article.getDistributionDate());
    TextView tvShortContent = (TextView) findViewById(R.id.tvShortContent);
    tvShortContent.setText(article.getShortContent());
    // setting web view
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            OSUtil.gotoURL(getActivity(), url);
            return true;
        }
    });
    webView.getSettings()
            .setUserAgentString(
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");

    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    // get view
    layoutReload = findViewById(R.id.layoutReload);
    progressBar = findViewById(R.id.layoutProgress);
    findViewById(R.id.btnShare).setOnClickListener(this);
    findViewById(R.id.btnReload).setOnClickListener(this);
    findViewById(R.id.btnBack).setOnClickListener(this);
    loadContentArticle();
}

有日志:

08-07 03:40:38.069: W/dalvikvm(2193): threadid=1: thread exiting with uncaught exception (group=0xb4c73908)
08-07 03:40:38.069: E/AndroidRuntime(2193): FATAL EXCEPTION: main
08-07 03:40:38.069: E/AndroidRuntime(2193): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.addView(ViewGroup.java:3186)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at com.vmodev.cpdt.fragments.base.BaseFragment.onCreateView(BaseFragment.java:20)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.onMeasure(CustomViewAbove.java:456)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.View.measure(View.java:15513)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.Choreographer.doFrame(Choreographer.java:532)
08-07 03:40:38.069: E/AndroidRuntime(2193):     at android.view.Choreographer$FrameDisplayEventR

和我的布局:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_action_bar_bak"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_back_light" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="16sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/btnShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_share_light" />
</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvTitle"
            style="@style/styleTextTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvDatetime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/tvShortContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:padding="5dp"
            android:textSize="16sp"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/layoutReload"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/error_lost_internet_connection" />

            <Button
                android:id="@+id/btnReload"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="@string/btnReload" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutProgress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp" >

            <ProgressBar
                style="?android:attr/progressBarStyleLargeInverse"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/msg_loading" />
        </LinearLayout>

        <com.vmodev.cpdt.views.HTML5WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="2dp" />
    </LinearLayout>
</ScrollView>

4

1 回答 1

3

使用removeAllViewsInLayout();代替removeAllViews()

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (view != null) {
        ViewGroup parentViewGroup = (ViewGroup) view.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViewsInLayout();;
        }
    }
}

我不确定 onDestroyView() 是唯一应该调用它的地方。但我建议在向parentViewGroup您添加视图之前应该调用parentViewGroup.removeAllViewsInLayout();;以避免运行时异常。

于 2013-08-07T04:35:23.803 回答