带有listview的Android选项卡片段

时间:2012-10-04 09:01:42

标签: android listview tabs android-asynctask android-fragments

尝试运行我的应用时遇到错误。有人可以帮我查一下我的代码吗?我会列出我的所有代码。我正在尝试将片段应用到我的应用中。

MainActivity.java

public class MainActivity extends SherlockFragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setDisplayShowTitleEnabled(true);

        Tab tab = actionBar.newTab()
                .setText("Apple")
                .setTabListener((TabListener) new AppleFragment())
                .setIcon(R.drawable.apple);

        actionBar.addTab(tab);


    }  

}

AppleFragment.java

public class AppleFragment extends ListActivity  {

    static final String URL = "http://api.androidhive.info/pizza/?format=xml";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_NAME = "name";
    static final String KEY_COST = "cost";
    static final String KEY_DESC = "description";

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

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();


    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });


    setListAdapter(adapter);



}

    public static class ArrayListFragment extends SherlockListFragment implements TabListener{


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




        return super.onCreateView(inflater, container, savedInstanceState);


    }    


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(android.R.id.content, this);

        ft.attach(this);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.detach(this);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

}
}

Logcat:

10-04 16:55:52.805: E/AndroidRuntime(26238): FATAL EXCEPTION: main
10-04 16:55:52.805: E/AndroidRuntime(26238): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.ClassCastException: in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment cannot be cast to com.actionbarsherlock.app.ActionBar$TabListener
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.os.Looper.loop(Looper.java:137)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread.main(ActivityThread.java:4512)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at java.lang.reflect.Method.invokeNative(Native Method)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at java.lang.reflect.Method.invoke(Method.java:511)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at dalvik.system.NativeStart.main(Native Method)
10-04 16:55:52.805: E/AndroidRuntime(26238): Caused by: java.lang.ClassCastException: in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment cannot be cast to com.actionbarsherlock.app.ActionBar$TabListener
10-04 16:55:52.805: E/AndroidRuntime(26238):    at in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity.onCreate(MainActivity.java:30)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.Activity.performCreate(Activity.java:4465)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-04 16:55:52.805: E/AndroidRuntime(26238):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-04 16:55:52.805: E/AndroidRuntime(26238):    ... 11 more

2 个答案:

答案 0 :(得分:1)

错误在:

Tab tab = actionBar.newTab()
                .setText("Apple")
                .setTabListener((TabListener) new AppleFragment())
                .setIcon(R.drawable.apple);

标签监听器不能new AppleFragment()你应该传递一个有效的监听器,或者让你的片段实现TabListener

答案 1 :(得分:0)

在Apple片段中扩展Fragment类而不是ListFragment