2014-02-08 22 views
0

我想指的是使用交換機的方法特異性片段,並指特定片段時,在IDE(Eclipse中)拋出的錯誤時。錯誤在SectionsPagerAdapter類指特定片段

特定的錯誤是:類型不匹配:不能從GuidelineFragment轉換到碎片

我的第一個懷疑的是,我被延長的GuidelineFragment到ListActivity的活動,但是當我擴展到ListFragment拋出更多的錯誤並恢復到ListActivity擴展。

我組織進口和清理沒有成功的項目。

不幸的是,以下的StackOverflow問題並沒有明確回答我的問題:

Android Fragments for Tabbed Menu

Problems with the tabs and the fragment

我在做什麼錯?

下面是從我的項目

SectionsPagerAdapter類文件

package com.example.perinatologiev2; 

import java.util.Locale; 

import com.example.perinatologiev2.R; 

import android.content.Context; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    protected Context mContext; 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 

    public SectionsPagerAdapter(Context context,FragmentManager fm) { 
     super(fm); 
     mContext = context; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page.. 
     switch(position) { 
     case 0: 
      return new GuidelineFragment(); //error here 
     case 1: 
      return new CentraFragment(); //error here 
     case 2: 
      return new UzitecneFragment(); //error here 
     } 

     return null; 

    } 

    @Override 
    public int getCount() { 
     // Showing 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
      return mContext.getString(R.string.title_section1).toUpperCase(l); 
     case 1: 
      return mContext.getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return mContext.getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 

這是switch語句引用片段:

GuidelineFragment

package com.example.perinatologiev2; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONTokener; 

import com.example.perinatologiev2.R; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class GuidelineFragment extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_guideline_fragment); 
     try 
     { 
      JSONArray jsonArray = parsePostupyJSON(); 
      ArrayList<String> fields = new ArrayList<String>(); 
      for (int index = 0; index < jsonArray.length(); index++) { 
       //Set both values into the listview 
       JSONObject jsonObject = jsonArray.getJSONObject(index); 
       fields.add(jsonObject.getString("title")); 
      } 

      setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fields)); 
     } catch (FileNotFoundException e) { 
      Log.e("jsonFile", "file not found"); 
     } catch (IOException e) { 
      Log.e("jsonFile", "ioerror"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.guideline, menu); 
     return true; 
    } 

    private JSONArray parsePostupyJSON() throws IOException, JSONException { 
     //Load File 
     BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.guidelines))); 

     StringBuilder jsonBuilder = new StringBuilder(); 
     for (String line = null; (line = jsonReader.readLine()) != null;) { 
     jsonBuilder.append(line).append("\n"); 
     } 

     //Parse Json 
     JSONTokener tokener = new JSONTokener(jsonBuilder.toString()); 
     JSONArray jsonArray = new JSONArray(tokener); 
     return jsonArray; 
    } 


    //The following is the method that I need to modify to listen to the tap and direct to web view 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     try { 
      JSONArray jsonArray = parsePostupyJSON(); 
      JSONObject jsonPost = jsonArray.getJSONObject(position); 
      String guidelineUrl = jsonPost.getString("guidelinepath"); 
      Intent intent = new Intent(this, GuidelineWebActivity.class); 
      intent.setData(Uri.parse(guidelineUrl)); 
      startActivity(intent); 
     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

而且最後但並非最不重要的....主要活動

package com.example.perinatologiev2; 

import com.example.perinatologiev2.R; 

import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.Menu; 

public class MainActivity extends FragmentActivity implements 
     ActionBar.TabListener { 

    public static final String TAG = MainActivity.class.getSimpleName(); 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 

    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 

    ViewPager mViewPager; 

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

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

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(this, 
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the 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++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @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 onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

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


} 
+0

究竟是什麼錯誤? –

+0

嗨@MaciejGórski,很抱歉回覆晚了!確切的錯誤是:類型不匹配:無法從GuidelineFragment轉換爲片段 – stefbmt

回答

1

如果你想使用GuidelineFragment(其他類)內ViewPager,他們需要延長Fragment而不是Activity。這兩種方法有不同的方法,你需要重寫,這就是爲什麼你在改變超類後會出現如此多的錯誤,例如你Fragment!而非ActivityonCreate工作時,通常使用onCreateView

+0

同意,事實上,當我將擴展名從ListActivity更改爲ListFragment時,錯誤消失了。 關於如何使用本地存儲的JSON填充列表片段的好資源?先謝謝您的幫助 :)! – stefbmt

+0

@stefbmt'「我如何用本地存儲的JSON填充列表片段」'不是一個好的SO問題。當詢問您是否在解析或爲ListView創建適配器時遇到問題時,請具體說明。分而治之!並且玩得開心學習Android。 –

+0

感謝您的反饋。我仍然有很多要學習! – stefbmt