View Pager中的IllegalArgumentException

时间:2014-04-07 18:32:26

标签: java android android-fragments

我正在尝试将三个片段加载到一个活动中。出于某种原因,我得到IllegalStateException,我不知道为什么。以下是FragmentActivity的代码:

public class HelloAndroidActivity extends SherlockFragmentActivity {

private ViewPager pager;
private TabAdapter tabAdapter;
private TabHost tHost;

private ActionBar bar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);  

    tHost = (TabHost)findViewById(android.R.id.tabhost);
    tHost.setup();

    bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(false);
    bar.setTitle("Your List");

    pager = (ViewPager) findViewById(R.id.listFragmentPager);

    Bundle extras = new Bundle();
    extras.putString("uri", "table_1");
    extras.putString("print", "BALL");

    tabAdapter = new TabAdapter(this, tHost, pager);
    tabAdapter.addTab(tHost.newTabSpec("Table 1").setIndicator(createTabView(tHost.getContext(), "Table 1")), OnlyFragment.class, extras);
    tabAdapter.addTab(tHost.newTabSpec("Table 2").setIndicator(createTabView(tHost.getContext(), "Table 2")), OnlyFragment.class, extras);
    tabAdapter.addTab(tHost.newTabSpec("Table 3").setIndicator(createTabView(tHost.getContext(), "Table 3")), OnlyFragment.class, extras);

}

public static class TabAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener
{
    private final Context context;
    private final ArrayList<TabInfo> tabList = new ArrayList<TabInfo>();
    private final ViewPager viewPager;
    private Map<Integer, String> fragTags;
    private final TabHost tabHost;
    FragmentManager manager = null;

    static final class TabInfo {


        private final Class<?> clss;
        private final Bundle args;
        private final String tag;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {

            tag = _tag;
            clss = _class;
            args = _args;
        }
    }


    public TabAdapter(FragmentActivity activity, TabHost host, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        manager = activity.getSupportFragmentManager();
        context = activity;
        tabHost = host;
        viewPager = pager;
        tabHost.setOnTabChangedListener(this);
        viewPager.setAdapter(this);
        viewPager.setOnPageChangeListener(this);

        fragTags = new HashMap<Integer, String>();
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {

        private final Context ctx;

        public DummyTabFactory(Context context) {

            ctx = context;
        }

        @Override
        public View createTabContent(String tag) {

            View v = new View(ctx);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
    {
        if(tabSpec != null && tabHost != null)
        {
            tabSpec.setContent(new DummyTabFactory(context));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            tabList.add(info);
            tabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }
    }

    @Override
    public Fragment getItem(int pos)
    {
        TabInfo info = tabList.get(pos);

        if(pos == 0)
        {
            info.args.putString("uri", "tabel_1");
            info.args.putString("print", "BALL");
        }
        if(pos == 1)
        {
            info.args.putString("uri", "table_2");
            info.args.putString("print", "PIN");
        }
        if(pos == 2)
        {
            info.args.putString("uri", "table_3");
            info.args.putString("print", "DRIVEWAY");
        }

        OnlyFragment frag = (OnlyFragment)Fragment.instantiate(context, info.clss.getName(), info.args);

        return frag;
    }

    @Override
    public int getCount()
    {
        return 3;
    }

    @Override 
    public void onPageScrollStateChanged(int state)
    {

    }

    @Override 
    public void onPageSelected(int pos)
    {

    }

    @Override 
    public void onPageScrolled(int pos, float howFar, int newPos)
    {
        //Auto generated
    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub

    }

}

private static View createTabView(final Context context, final String text) {

    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);

    TextView tv = (TextView) view.findViewById(R.id.tabsText);

    tv.setText(text);

    return view;
}

这是片段的代码。

public class OnlyFragment extends Fragment
{
private FragmentDatabaseHelper dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
private ListView listTemp;
private FragmentAdapter adapter;
private Context context;
private Uri tableName;
private String print;
private int loaderID;


static OnlyFragment newInstance(Bundle bundle)
{
    OnlyFragment f = new OnlyFragment();

    f.setArguments(bundle);

    return f;
}

@Override
public void onCreate(Bundle savedInstance)
{
    super.onCreate(savedInstance);
}

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

    super.onActivityCreated(onSavedInstance);

    getActivity().setContentView(R.layout.only_fragment);

    listTemp = (ListView)getActivity().findViewById(R.id.listTest);

    context = getActivity().getApplicationContext();

    Bundle extras = getArguments();

    if(extras.getString("uri") == "table_1")
    {
        tableName = FragmentProvider.TABLE_1_URI;
        print = "BALL";
    } else if(extras.getString("uri") == "table_2")
    {
        tableName = FragmentProvider.TABLE_2_URI;
        print = "PIN";
    } else if(extras.getString("uri") == "table_3")
    {
        tableName = FragmentProvider.TABLE_3_URI;
        print = "DRIVEWAY";
    }


    dbHelper = new FragmentDatabaseHelper(context);

    db = dbHelper.getWritableDatabase();
    insertIntoDb();

    cursor = context.getContentResolver().query(tableName, null, null, null, null);

    adapter = new FragmentAdapter(context, cursor);
    listTemp.setAdapter(adapter);

    View view = inflater.inflate(R.layout.only_fragment, container, false);


    return view;
}

@Override 
public void onActivityCreated(Bundle savedInstance)
{
    super.onActivityCreated(savedInstance);
}

private void insertIntoDb()
{
    ContentValues values = new ContentValues();
    for(int i = 0; i<5; i++)
    {
        values.put("name", print);
        context.getContentResolver().insert(tableName, values);
    }
}

这是stacktrace:

 java.lang.IllegalArgumentException: No view found for id 0x7f040032 (your.company:id/pager) for fragment OnlyFragment{4231cd90 #1 id=0x7f040032 android:switcher:2130968626:1}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16537)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16537)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16537)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1947)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1326)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

我已将问题缩小到寻呼机适配器中的viewPager.setAdapter(this);

感谢任何更多帮助

修改

这是activity_main的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"  >

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="#000000"
            android:gravity="center_vertical"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:color="#FFFFFF" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/listFragmentPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
</TabHost> 

修改2

另一个布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView 
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:id="@+id/listTest"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我会冒险猜测。您的R.id.listFragmentPager文件中是否有R.layout.activity_main?仔细检查文件,确保将其输入为id