设置ListAdapter

时间:2016-01-28 01:50:43

标签: java android listview

当我尝试将我的适配器链接到ListView

时,我的Android应用程序崩溃了

这是我的Activty

package needforbleed.com.music;

import android.content.Context;
import android.provider.MediaStore;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.net.Uri;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.ListView;

import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  //vars
    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    public ArrayList<Song> songList;

    private ListView songView;





    //methods


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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        songList=new ArrayList<Song>();
        songView = (ListView)findViewById(R.id.lv_titles);

        getSongList();

        Collections.sort(songList, new Comparator<Song>() {
            public int compare(Song a, Song b) {
                return a.getName().compareTo(b.getName());
            }
        });


        SongAdapter songAdt = new SongAdapter(this,R.id.lv_titles, songList);
        songView.setAdapter(songAdt);


       /* for(int x=0;x<songList.size();x++) {
            Toast toast = Toast.makeText(this,songList.get(x).getName(),Toast.LENGTH_SHORT);
            toast.show();

        }*///I used that loop to check songList for Content. It has content so that's not the error.
    }





    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
           switch (position)
           {
               case 0:return Player.newInstance(0,"Player");
               case 1:return Artists.newInstance(1,"Artists");
               case 2:return Albums.newInstance(2,"Albums");
               case 3:return Titles.newInstance(3,"Titles");

           }
          return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Player";
                case 1:
                    return "Artists";
                case 2:
                    return "Albums";
                case 3:
                    return "Titles";
            }
            return null;
        }
    }

    public void getSongList() {
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);

            int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);

            int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);

           int albumColumn=musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String thisAlbum = musicCursor.getString(albumColumn);
                Song s=new Song();
                s.setID(thisId);
                s.setName(thisTitle);
                s.setArtist(thisArtist);
                s.setAlbum(thisAlbum);

                songList.add(s);
            }
            while (musicCursor.moveToNext());
        }

    }
}

这是我的Custom ListAdapter

package needforbleed.com.music;


import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created on 27.01.2016.
 */

    public class SongAdapter extends ArrayAdapter<Song> {

        public SongAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public SongAdapter(Context context, int resource, ArrayList<Song> tracks) {
            super(context, resource, tracks);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.title_list,null);
            }

            Song p = getItem(position);

            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.song_title);
                TextView tt2 = (TextView) v.findViewById(R.id.song_artist);
                TextView tt3 = (TextView) v.findViewById(R.id.song_album);

                if (tt1 != null) {
                    tt1.setText(p.getName());
                }

                if (tt2 != null) {
                    tt2.setText(p.getArtist());
                }

                if (tt3 != null) {
                    tt3.setText(p.getAlbum());
                }
            }

            return v;
        }

    }

这是包含listView的XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="needforbleed.com.music.Titles">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="422dp"
        android:id="@+id/lv_titles"
        android:layout_gravity="left|top" />
</FrameLayout>

我出错的错误是:

  

01-28 02:41:46.635 24445-24445 / needforbleed.com.music   E / AndroidRuntime:致命异常:主要                                                                           过程:needforbleed.com.music,PID:24445                                                                           java.lang.RuntimeException:无法启动活动   ComponentInfo {needforbleed.com.music / needforbleed.com.music.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.ListView.setAdapter(android.widget.ListAdapter)'上的一个   null对象引用                                                                               在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)                                                                               在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)                                                                               在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                                                                               在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1359)                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                               在android.os.Looper.loop(Looper.java:155)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5696)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在java.lang.reflect.Method.invoke(Method.java:372)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)                                                                            引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.widget.ListView.setAdapter(android.widget.ListAdapter)'上的一个   null对象引用                                                                               at needforbleed.com.music.MainActivity.onCreate(MainActivity.java:92)                                                                               在android.app.Activity.performCreate(Activity.java:5958)                                                                               在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)                                                                               在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)                                                                               在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)                                                                               在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                                                                               在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1359)                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                               在android.os.Looper.loop(Looper.java:155)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5696)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在java.lang.reflect.Method.invoke(Method.java:372)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

我真的不知道什么是null。 我甚至尝试过这里的解决方案,但无论我遵循什么教程,我都会得到同样的错误。

1 个答案:

答案 0 :(得分:1)

SongAdapter songAdt = new SongAdapter(this,R.id.lv_titles, songList);
        songView.setAdapter(songAdt);

应该是

SongAdapter songAdt = new SongAdapter(this,R.layout.layoutId, songList);
        songView.setAdapter(songAdt);

其中layoutId是包含在实例化视图时使用的TextView的布局文件的资源ID。

你可以使用android提供的默认listitem布局,使用&#34; android.R.layout.simple_list_item_1&#34;作为layoutId