java - Android 非法状态异常 : The specified child already has a parent

标签 java android illegalstateexception swiperefreshlayout

我收到标题中所述的 IllegalStateException,我知道这是因为我在某个地方添加了一个已经有父级的 View ,但我找不到在哪里?我已经尝试了一些不同的解决方案,为其他遇到此问题的人找到了解决方案,但它没有起作用,因为他们似乎是从比我更好的另一个来源得到这个问题的。非常感谢您的帮助!

错误堆栈:

10-26 17:46:54.783: E/AndroidRuntime(14255): FATAL EXCEPTION: main
10-26 17:46:54.783: E/AndroidRuntime(14255): Process: com.example.digiexammobile, PID: 14255
10-26 17:46:54.783: E/AndroidRuntime(14255): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.addView(ViewGroup.java:3415)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.addView(ViewGroup.java:3360)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.addView(ViewGroup.java:3336)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:951)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.View.measure(View.java:16521)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.View.measure(View.java:16521)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.View.measure(View.java:16521)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.View.measure(View.java:16521)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.Choreographer.doFrame(Choreographer.java:544)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.os.Handler.handleCallback(Handler.java:733)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.os.Looper.loop(Looper.java:136)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at android.app.ActivityThread.main(ActivityThread.java:5146)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at java.lang.reflect.Method.invokeNative(Native Method)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at java.lang.reflect.Method.invoke(Method.java:515)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
10-26 17:46:54.783: E/AndroidRuntime(14255):    at dalvik.system.NativeStart.main(Native Method)

我在此处找不到对我的任何文件的任何引用?

我正在标准选项卡式应用程序之上构建(来自 Eclipse 创建应用程序指南),但我对支持库替代方案进行了一些更改,以便将其与 SwipeRefreshLayout 一起使用。

我的MainActivity.java:

package com.example.digiexammobile;

import java.util.Locale;

import android.app.ActionBar.Tab;
import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v13.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;
    NWManager nwmanager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        nwmanager = new NWManager();

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());

    }


    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        Fragment[] fragments = new Fragment[this.getCount()];

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            if(fragments[position] == null){
                fragments[position] = (Fragment) LibraryListFragment.newInstance(nwmanager);
            }

            return fragments[position];
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
            }
            return null;
        }
    }
}

LibraryListFragment.java:

package com.example.digiexammobile;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class LibraryListFragment extends Fragment {

    BaseAdapter listadapter;
    NWManager nwmanager;

    public static LibraryListFragment newInstance(NWManager nwmanager){
        LibraryListFragment fragment = new LibraryListFragment();
        fragment.nwmanager = nwmanager;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_librarylist, container);

        final String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" }; // You have the necessary data to bind the list.

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, android.R.id.text1, values); // You have set     the previous array to an adapter that can be now setted to a list.

        ListView list = (ListView) rootView.findViewById(R.id.librarylist);
        list.setAdapter(adapter);

        return rootView;
    }

    public void notifyDataSetChanged(){
        listadapter.notifyDataSetChanged();
    }

}

最后是我的fragment_librarylist.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/librarylist">
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

非常感谢!

最佳答案

LibraryListFragmentonCreateView 中,更改以下内容:

View rootView = inflater.inflate(R.layout.fragment_librarylist, container);

对此:

View rootView = inflater.inflate(R.layout.fragment_librarylist, container, false);

关于java - Android 非法状态异常 : The specified child already has a parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26575629/

相关文章:

android - 如何获得 ListView 的焦点子项?

android - 在 android 3.x 中拖放少量后会导致 illegalStateException

android - fragment IllegalStateException - commit() 导致状态丢失

java - 为什么我们需要接口(interface)而不是类以及我们从接口(interface)中实现了什么

java - 是否可以在 Java 的 print 语句中添加 sleep() 方法?

java - 如何使 Java GUI settext 识别二维数组输入的换行符

android - adapter 的内容发生了变化但是 ListView 没有收到通知,后台没有更新

java - 如何生成读取文本的 FTL 代码?

android - Smack 4.1 所需的最低 Android API 级别是多少?

java - 请求另一个 EditText 的焦点行为异常