ビューページャーとフラグメントページャーアダプターを使用して、スライドビュー/フラグメントを実装しようとしています。convert_home は、android.support.v4.view.PagerTitleStrip を含む私のメインの xml ファイルです。
温度.xml と速度.xml は、私の他の 2 つのビューです。どこが間違っているのかわかりません。アプリがクラッシュしています (ロード中)
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
}