7

Ho guardato un bel po 'di codice e non riesco a capirlo. http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.htmlFragmentPagerAdapter errore getItem con ListFragment

Deve essere qualcosa di semplice. Sto mostrando la maggior parte del codice. L'errore è nella prossima sezione.

public class MainActivity extends FragmentActivity implements 
    ActionBar.TabListener { 

public static final int TAB_COUNT = 3; 
public static InputMethodManager inputManager; 

SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(
      getSupportFragmentManager()); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    // When swiping between different sections, select the corresponding 
    // tab. 
    mViewPager 
      .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
       @Override 
       public void onPageSelected(int position) { 
        actionBar.setSelectedNavigationItem(position); 
       } 
      }); 

    // For each of the sections in the app, add a tab to the action bar. 
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 

     actionBar.addTab(actionBar.newTab() 
       .setText(mSectionsPagerAdapter.getPageTitle(i)) 
       .setTabListener(this)); 
    } 

    // To control keyboard pop-up 
    inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

} 
    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
} 

Questo è dove si trova il mio errore. caso 1 di getItem, Tipo non corrispondente: Impossibile convertire da MainActivity.HistoryFragment a Frammento. mi sta dicendo di cambiare il tipo di metodo restituito a HistoryFragment o cambiare il tipo di ritorno di newInstance() in Frammento. Dove non posso fare neanche io. Altri esempi che ho visto sembrano quasi identici al mio codice. Ho provato con e senza passare una discussione.

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     switch (i) { 
     case 0: 
      Fragment tipCalc = new TipCalcFragment(); 
      return tipCalc; 
     case 1: 
      // Fragment history = new HistoryFragment(); 
      return HistoryFragment.newInstance(i); //ERROR HERE 
     case 2: 
      Fragment stats = new StatsFragment(); 
      return stats; 
     } 
     return null; 
    } 

E la mia HistoryFragment che si estende ListFragment. Alla fine non verrà prelevato dai valori String[] ma dalle risorse del database. Volevo solo prima configurare una struttura e vederla/giocare con il layout.

public static class HistoryFragment extends ListFragment { 
    // public HistoryFragment() { 
    // } 

    String[] values = new String[] { ... }; 

    static HistoryFragment newInstance(int num) { 
     HistoryFragment history = new HistoryFragment(); 

     Bundle args = new Bundle(); 
     args.putInt("num", num); 

     history.setArguments(args); 

     return history; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.history, container, false); 

     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, android.R.id.text1, 
       values)); 
    } 
} 

risposta

26

L'ho capito dopo aver dormito un po '. Stavo importando android.app.ListFragment invece di android.support.v4.app.ListFragment

+1

imposta il framework del progetto su 2.3.3 (se non hai bisogno di roba condizionale di 3+) in modo da evitare questo tipo di problemi – sherpya

+0

Dovresti contrassegnare la risposta come "accettata" se questo ha risolto la tua domanda. – Luis

+0

Lo farò. Dice che devo aspettare fino a domani. Grazie a tutti e due. – user1235035

0

Penso che si dovrà digitare a frammento prima di tornare. Quindi prova questo

Fragment history = HistoryFragment.newInstance(i); 
return history; 
+0

Ho provato e no. Ho aggiunto un link ad un esempio. – user1235035

Problemi correlati