2016-01-28 115 views
0
当我想我的适配器连接到ListView

Android的Java应用程序设置ListAdapter

我的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()); 
     } 

    } 
} 

这是我的自定义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> 

的误差即时得到的是:

2月1日至28日: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)' 空对象引用 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 在android.app.ActivityThread.access $ 800(ActivityThread.java:144 ) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1359) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java: 155) at android.app.ActivityThread.main(ActivityThread.java:5696) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:824) 引起:显示java.lang.NullPointerException:尝试一个 空对象上调用虚拟方法 '空隙 android.widget.ListView.setAdapter(android.widget.ListAdapter)'参考 at needforbleed.com.music.MainActivity.onCreate(MainActivity.java:92) at android.app.Activity.performCreate(Activity.java:5958) at a ndroid.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) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1359) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread。java:5696) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

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

+0

'songView'为空透过Android提供的默认列表项的布局? –

+0

我猜不。我引用它:“songView =(ListView)findViewById(R.id.lv_titles);”但是你知道一种方法来检查吗? – Needforbleed

+0

你确定''getSongList''函数完成并返回值,然后再初始化适配器? –

回答

0
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.R.layout.simple_list_item_1”作为layoutId

相关问题