所以我正在尝试使用 viewpager 和片段寻呼机适配器来实现滑动视图/片段。convert_home 是我的主要 xml 文件,它具有 android.support.v4.view.PagerTitleStrip 和
temperature.xml 和velocity.xml 是我的另外两个观点。我不知道我哪里出错了。应用程序崩溃(加载时)
package app.converto;
import android.os.Bundle;
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.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ConverTo extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
setContentView(R.layout.converto_home);
mViewPager = (ViewPager) findViewById(R.id.pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.converto_home, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch(i){
case 0:
Fragment1 fragment = new Fragment1();
return fragment;
case 1:
Fragment2 fragment2 = new Fragment2();
return fragment2;
}
defaultFragment fragment3 = new defaultFragment();
return fragment3;
}
@Override
public int getCount() {
return 2;
}
//
// @Override
// public CharSequence getPageTitle(int position) {
// switch (position) {
// case 0: return getString(R.string.velocity);
// case 1: return getString(R.string.temperature);
// case 2: return getString(R.string.distance);
// }
// return null;
// }
}
public static class Fragment1 extends Fragment{
public Fragment1(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//return inflater.inflate(R.layout.temperature, container, false);
View view = inflater.inflate(R.layout.temperature, container, false);
TextView textView = (TextView) view.findViewById(R.id.sample);
textView.setText(getArguments().getString("title"));
return view;
}
}
public static class Fragment2 extends Fragment{
public Fragment2(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//return inflater.inflate(R.layout.velocity, container, false);
View view = inflater.inflate(R.layout.temperature, container, false);
TextView textView = (TextView) view.findViewById(R.id.sample);
textView.setText(getArguments().getString("title"));
return view;
}
}
public static class defaultFragment extends Fragment{
public defaultFragment(){
}//end constructor
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// return inflater.inflate(R.layout.temperature, container, false);
View view = inflater.inflate(R.layout.temperature, container, false);
TextView textView = (TextView) view.findViewById(R.id.sample);
textView.setText(getArguments().getString("title"));
return view;
}//end oncreate
}//end default fragment
}