在ActionBar Tab片段中创建ListView

时间:2013-11-25 10:51:38

标签: android listview android-listview android-fragments

我正在尝试在ActionBar标签内创建一个ListView,找不到合适的教程来帮助我创建它。

任何sugestions?

大部分代码已经从教程中删除,但会导致以下错误:

11-25 12:44:29.875: D/libEGL(20751): loaded /system/lib/egl/libEGL_mali.so
11-25 12:44:29.885: D/libEGL(20751): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-25 12:44:29.890: D/libEGL(20751): loaded /system/lib/egl/libGLESv2_mali.so
11-25 12:44:29.895: D/(20751): Device driver API match
11-25 12:44:29.895: D/(20751): Device driver API version: 10
11-25 12:44:29.895: D/(20751): User space API version: 10 
11-25 12:44:29.895: D/(20751): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
11-25 12:44:29.930: D/OpenGLRenderer(20751): Enabling debug mode 0
11-25 12:44:30.045: D/AndroidRuntime(20751): Shutting down VM
11-25 12:44:30.045: W/dalvikvm(20751): threadid=1: thread exiting with uncaught exception (group=0x414fe2a0)
11-25 12:44:51.650: D/libEGL(21151): loaded /system/lib/egl/libEGL_mali.so
11-25 12:44:51.660: D/libEGL(21151): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-25 12:44:51.665: D/libEGL(21151): loaded /system/lib/egl/libGLESv2_mali.so
11-25 12:44:51.665: D/(21151): Device driver API match
11-25 12:44:51.665: D/(21151): Device driver API version: 10
11-25 12:44:51.665: D/(21151): User space API version: 10 
11-25 12:44:51.665: D/(21151): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
11-25 12:44:51.695: D/OpenGLRenderer(21151): Enabling debug mode 0
11-25 12:44:51.865: D/AndroidRuntime(21151): Shutting down VM
11-25 12:44:51.865: W/dalvikvm(21151): threadid=1: thread exiting with uncaught exception (group=0x414fe2a0)
11-25 12:44:51.880: E/AndroidRuntime(21151): FATAL EXCEPTION: main
11-25 12:44:51.880: E/AndroidRuntime(21151): java.lang.RuntimeException: Unable to start activity ComponentInfo{rtd.nightoutin.stellenbosch/rtd.nightoutin.stellenbosch.MainActivity}: java.lang.NullPointerException
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.os.Looper.loop(Looper.java:137)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at dalvik.system.NativeStart.main(Native Method)
11-25 12:44:51.880: E/AndroidRuntime(21151): Caused by: java.lang.NullPointerException
11-25 12:44:51.880: E/AndroidRuntime(21151):    at rtd.nightoutin.stellenbosch.MainActivity.onCreate(MainActivity.java:79)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.Activity.performCreate(Activity.java:5206)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-25 12:44:51.880: E/AndroidRuntime(21151):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-25 12:44:51.880: E/AndroidRuntime(21151):    ... 11 more
11-25 12:44:51.905: D/dalvikvm(21151): GC_CONCURRENT freed 135K, 9% free 12408K/13511K, paused 3ms+2ms, total 23ms

我确实有用于扩展Fragment的Tabs的单独类。

选项卡100%工作,只有当我尝试添加ArrayAdapter和ListView时才会出现错误。

这是我到目前为止所做的。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity implements TabListener{

    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsPagerAdapter mAdapter;

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

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.addTab(actionBar.newTab()
                .setTabListener(this).setIcon(R.drawable.ic_tab_calendar));
        actionBar.addTab(actionBar.newTab()
                        .setTabListener(this).setIcon(R.drawable.ic_tab_favourite));
        actionBar.addTab(actionBar.newTab()
                .setTabListener(this).setIcon(R.drawable.ic_tab_venue));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        final ListView listview = (ListView) findViewById(R.id.listView_event_by_day);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }

        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }

}

非常感谢任何建议或教程。

2 个答案:

答案 0 :(得分:1)

试试这样..

    View view; 
    LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.fragment_event_by_day, null);


final ListView listview = (ListView) view.findViewById(R.id.listView_event_by_day);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);

答案 1 :(得分:-1)