我正在同时使用片段和ViewPagers。
The Issue Is
它正常工作,直到我尝试通过代码将更多片段添加到新布局中。
这是我的Video_Fragment类代码
import android.support.v4.app.Fragment;
public class Video_fragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final Video_fragment newInstance(String message)
{
Video_fragment f = new Video_fragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View view = inflater.inflate(R.layout.video_fragment, container, false);
TextView messageTextView = (TextView)view.findViewById(R.id.textView);
messageTextView.setText(message);
if(message.equals("Tv Shows"))
{
}
if(message.equals("Movies"))
{
}
if(message.equals("Music Videos"))
{
}
return view;
}
以下是我的Pager类的代码
public class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
My Question Is
当我尝试使用这段代码通过代码输入新片段时,我不断收到错误SOURCE NOT FOUND
Video_content = (LinearLayout)findViewById(R.id.Content);
android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
//Add first fragment
myFragment1 = new Video_fragment();
t.add(Video_content.getId(), myFragment1, "myFirstFragment");
myFragment2 = new Video_fragment();
t.add(Video_content.getId(), myFragment2, "mySecondFragment");
myFragment3 = new Video_fragment();
t.add(Video_content.getId(), myFragment3, "mySecondFragment");
t.commit();
为什么会出现此错误?
这是我的FragmentActivity的完整代码
public class Video extends SherlockFragmentActivity {
MyPageAdapter pageAdapter;
ViewPager pager;
LinearLayout Video_content;
public static Video_fragment myFragment1;
public static Video_fragment myFragment2;
public static Video_fragment myFragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(SampleList.THEME);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
setContentView(R.layout.activity_video);
Video_content = (LinearLayout)findViewById(R.id.Content);
android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
//Add first fragment
myFragment1 = new Video_fragment();
t.add(Video_content.getId(), myFragment1, "myFirstFragment");
myFragment2 = new Video_fragment();
t.add(Video_content.getId(), myFragment2, "mySecondFragment");
myFragment3 = new Video_fragment();
t.add(Video_content.getId(), myFragment3, "mySecondFragment");
t.commit();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
Button button1=(Button)findViewById(R.id.Tvshows);
Button button2=(Button)findViewById(R.id.movies);
Button button3=(Button)findViewById(R.id.music);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(0);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(0);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(1);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(2);
}
});
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(myFragment1);
fList.add(myFragment2);
fList.add(myFragment3);
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
menu.add("")
.setIcon(R.drawable.network)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("14°")
.setIcon(R.drawable.weather1)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("00:00")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
}