7

के साथ त्रुटि त्रुटि प्राप्त करें मैंने बहुत सारे कोड को देखा है और इसे समझ नहीं सकता है। http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.htmlFragmentPagerAdapter GetFtagment

यह कुछ आसान होना चाहिए। मैं अधिकतर कोड दिखा रहा हूं। त्रुटि अगले खंड में है।

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) { 
} 

यह मेरी त्रुटि है। getItem का केस 1, टाइप मिस्चैच: मेनएक्टिविटी से कनवर्ट नहीं किया जा सकता है। फ्रैगमेंट के लिए हिस्ट्रीफ्रैगमेंट। यह मुझे विधि वापसी प्रकार को HistoryFragment में बदलने या फ्रैगमेंट में newInstance() के वापसी प्रकार को बदलने के लिए कह रहा है। जहां मैं या तो नहीं कर सकता। मैंने देखा है कि अन्य उदाहरण मेरे कोड के लगभग समान दिखते हैं। मैंने तर्क के साथ और बिना प्रयास किए हैं।

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; 
    } 

और मेरे HistoryFragment कि ListFragment फैली हुई है। अंत में यह String[] मानों से नहीं बल्कि डेटाबेस संसाधनों से खींच रहा है। मैं बस पहले एक संरचना स्थापित करना चाहता था और लेआउट के साथ इसे/खेलना चाहता था।

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)); 
    } 
} 

उत्तर

26

मैंने कुछ नींद लेने के बाद इसे समझ लिया। मैं android.support.v4.app.ListFragment

+1

प्रोजेक्ट फ्रेमवर्क 2.3.3 पर सेट करें (यदि आपको 3+ की सशर्त सामग्री की आवश्यकता नहीं है) तो आप इस तरह की समस्याओं से बच सकते हैं – sherpya

+0

यदि आपको यह प्रश्न हल हो गया है तो आपको अपना जवाब "स्वीकृत" के रूप में चिह्नित करना चाहिए। – Luis

+0

मैं करूँगा। यह कहता है कि मुझे कल तक इंतजार करना है। आप दोनों के लिए धन्यवाद। – user1235035

0

मुझे लगता है कि आपको लौटने से पहले खंड में टाइप करना होगा। तो इस

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

की बजाय android.app.ListFragment आयात कर रहा था मैंने कोशिश की है और नहीं। मैंने एक उदाहरण के लिए एक लिंक जोड़ा। – user1235035