无法在android中的片段中填充listview

时间:2014-06-18 18:34:40

标签: android android-fragments android-listview

在片段中阅读了很多关于listview的问题和答案之后,我仍然无法在listview中填充数据。

这是我的代码: 的片段:

public class SongViewFragment extends Fragment {


    public ArrayList<String> slist;
    int temp;
    public View rootView;


    public SongViewFragment(int pos,ArrayList<String> data) {

        slist=data;
        temp=pos;

    }

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

        rootView = inflater.inflate(R.layout.activity_song_view_fragment, container, false);
       //rootview is not null

        ListView gv = (ListView) rootView.findViewById(R.id.listView);
       //listview exists in xml file.

        gv.setAdapter(
                new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1,slist)
        );

        gv.setEnabled(true);
        return rootView;
    }

}

activity_song_view_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:fastScrollAlwaysVisible="false"
        android:fastScrollEnabled="true"
        android:smoothScrollbar="true"
        android:textFilterEnabled="true" />
</LinearLayout>

logcat的:

06-18 23:54:11.970  11931-11931/com.my.testing.app V/dalvikvm﹕ +++ retrieving /system/lib/libmedia_jni.so OnLoad status
06-18 23:54:11.970  11931-11931/com.my.testing.app V/dalvikvm﹕ +++ earlier OnLoad(/system/lib/libmedia_jni.so) okay
06-18 23:54:12.010  11931-11931/com.my.testing.app D/AndroidRuntime﹕ Shutting down VM
06-18 23:54:12.010  11931-11931/com.my.testing.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2c10180)
06-18 23:54:12.010  11931-11931/com.my.testing.app D/AndroidRuntime﹕ procName from cmdline: com.my.testing.app
06-18 23:54:12.010  11931-11931/com.my.testing.app E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :com.my.testing.app
06-18 23:54:12.010  11931-11931/com.my.testing.app D/AndroidRuntime﹕ file written successfully with content: com.my.testing.app StringBuffer : ;com.my.testing.app
06-18 23:54:14.840  12102-12102/com.my.testing.app I/PGA﹕ New SOCKET connection: .my.testing.app (pid 12102, tid 12102)
06-18 23:54:15.640  12102-12102/com.my.testing.app V/dalvikvm﹕ +++ retrieving /system/lib/libmedia_jni.so OnLoad status
06-18 23:54:15.640  12102-12102/com.my.testing.app V/dalvikvm﹕ +++ earlier OnLoad(/system/lib/libmedia_jni.so) okay
06-18 23:54:15.670  12102-12102/com.my.testing.app D/AndroidRuntime﹕ Shutting down VM
06-18 23:54:15.670  12102-12102/com.my.testing.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2c10180)
06-18 23:54:15.670  12102-12102/com.my.testing.app I/Process﹕ Sending signal. PID: 12102 SIG: 9
06-18 23:54:15.670  12102-12102/com.my.testing.app D/AndroidRuntime﹕ procName from cmdline: com.my.testing.app
06-18 23:54:15.670  12102-12102/com.my.testing.app E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :com.my.testing.app
06-18 23:54:15.670  12102-12102/com.my.testing.app D/AndroidRuntime﹕ file written successfully with content: com.my.testing.app StringBuffer : ;com.my.testing.app
06-18 23:54:15.670  12102-12102/com.my.testing.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsListView.obtainView(AbsListView.java:2012)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
            at android.widget.ListView.onMeasure(ListView.java:1155)
            at android.view.View.measure(View.java:12723)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
            at android.view.View.measure(View.java:12723)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
            at android.view.View.measure(View.java:12723)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
            at android.view.View.measure(View.java:12723)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
            at android.view.View.measure(View.java:12723)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
            at android.view.View.measure(View.java:12723)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
            at android.view.View.measure(View.java:12723)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
            at dalvik.system.NativeStart.main(Native Method)

更新 填写功能 狭缝

public ArrayList<String> fillPlayList(Context c,int k) {


            absolutepath = new ArrayList<String>();
            msqlist = new ArrayList<String>();

            songTitle = new ArrayList<String>();


            final Cursor mCursor = c.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.MIME_TYPE}, null, null,
                    "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

            String songs_name = "";
            String mAudioPath = "";

            if (mCursor.moveToFirst()) {
                do {

                    String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));



                    songs_name=mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

 mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                    HashMap<String, String> song = new HashMap<String, String>();




                    msqlist.add(songs_name); //song file name
                    absolutepath.add(mAudioPath);//full file path

                    MediaMetadataRetriever md = new MediaMetadataRetriever();
                    md.setDataSource(mAudioPath);

                    songTitle.add(md.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));


                } while (mCursor.moveToNext());
            }

            mCursor.close(); //cursor has been consumed so close it


                return songTitle;
}

调用fillPlaylist通过构造函数

将数据发送到片段
 @Override
        public Fragment getItem(int position) {

            ArrayList<String> data=new ArrayList<String>();
              data=fillPlayList(getContext(),position);
            return new SongViewFragment(position,data);


        }

1 个答案:

答案 0 :(得分:0)

传递给数组适配器的null返回的列表中有fillPlayList()个元素。这解释了NPE。要获得修复帮助,请发布该方法的代码。