我正在做一个简单的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对象。
有什么想法吗?
答案 0 :(得分:0)
将Fragments的构造函数更改为:
myView = inflater.inflate(R.layout.frame_medicamentos_layout, container, false);