2016-01-28 48 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

相關問題