【问题标题】:Error on instance second fragment on viewPagerviewPager 上的实例第二个片段出错
【发布时间】:2014-07-01 13:31:07
【问题描述】:

我正在做一个简单的viewpager,我的主要是这样的FragmentActivity:

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

    Log.w("bug bug", " ------------------------------------------------- inicia");

    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Log.w("bug bug", " ------------------------------------------------- get act");

    fragmentos = new ArrayList<Fragment>();
    fragmentos.add(new FragmentLineas());
    fragmentos.add(new FragmentMedicamentos());


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(new CustomPagerAdapter(getSupportFragmentManager()));
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

片段:

package co.com.smartmedia.vademtg;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentLineas extends Fragment {

    private View myView;

    public FragmentLineas() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        myView = super.onCreateView(inflater, container, savedInstanceState);

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

        return myView;
    }

}

(另一个片段是相同的代码,但对象名称不同) 每个片段的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


</RelativeLayout>

我得到的错误:

07-01 08:09:40.168: E/FragmentManager(1888): No view found for id 0x7f060000 (co.com.smartmedia.vademtg:id/pager) for fragment FragmentMedicamentos{41c4cb78 #1 id=0x7f060000}

我不知道该怎么做,太简单了,但是当我创建第二个片段对象时:

fragmentos.add(new FragmentLineas());
fragmentos.add(new FragmentMedicamentos());  //this onee

出现错误,如果我交换这些行,错误将更改为第二个 Class 对象。

有什么想法吗?

【问题讨论】:

    标签: android layout android-viewpager fragment android-fragmentactivity


    【解决方案1】:

    将 Fragments 的构造函数更改为:

        myView = inflater.inflate(R.layout.frame_medicamentos_layout, container, false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      相关资源
      最近更新 更多