是否可以通过此方法从另一个类中获取ArrayList?

时间:2016-07-08 18:50:21

标签: android arraylist

我正在创建一个小型音乐播放器应用。我有两个名为“Tab3Tracks”和“NowPlaying”的类。我在“Tab3Tracks”类中创建了一个ArrayList。现在,我想访问在“NowPlaying”类中加载歌曲的arraylist。检查我的代码是否有错误。我在访问ArrayList时遇到错误。

Tab3Tracks类

public class Tab3Tracks extends ListFragment {

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

private ArrayList<String> trackslist = new ArrayList<>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab3tracks, container, false);


    final String trackid = MediaStore.Audio.Media._ID;
    final String trackno = MediaStore.Audio.Media.TRACK;
    final String trackname = MediaStore.Audio.Media.TITLE;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    ContentResolver cr = getContext().getContentResolver();
    final String[] projection = {trackid,trackno,trackname,path
    };
    final Cursor cursor = cr.query(uri,projection,null,null,null);
    if (cursor!=null){
        if(cursor.moveToFirst()){
            do{
                int trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                trackslist.add(cursor.getString(trackIndex));
            }while(cursor.moveToNext());
        }
    } cursor.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(),R.layout.playlist_item,R.id.songTitle,trackslist
    );
    setListAdapter(adapter);
    return v;
}


public ArrayList<String> getList(){
    return trackslist;
}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    int songIndex = position;

    // Starting new intent
    Intent in = new Intent(getActivity(), NowPlaying.class);
    // Sending songIndex to PlayerActivity
    in.putExtra("songIndex", songIndex);
    getActivity().setResult(100, in);
    getActivity().finish();
    startActivity(in);
}
}

我的方法在从另一个类访问ArrayList时是否正确?

NowPlaying Class

  public class NowPlaying extends AppCompatActivity{

 public ArrayList<String> songsList = new ArrayList<String>();
 public Tab3Tracks songs1;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);
    // Getting all songs list
    songsList= songs1.getList();

 }
 }

LogCat运行时错误

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.digital.thefearstuff.tfsmusicplayer.Tab3Tracks.getList()' on a null object reference

1 个答案:

答案 0 :(得分:2)

您正在以错误的方式调用方法getlist()。你可以通过两种方法得到这个:

1)在所需的类中创建类的实例(tab3tracks),然后调用方法getlist()。

Tab3tracks tab3 = new Tab3Tracks(); songslist = tab3.getlist();

2)使方法成为静态,然后从另一个类调用它而不创建类的实例(tab3tracks)(其中包含方法getlist())

Tab3tracks.getlist();

您提出的错误是您没有将getlist()设为静态,而是在统计上调用它。

  

第一个解决方案---

Tab3Track类 - :

public class Tab3Tracks extends ListFragment {

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

public ArrayList<String> trackslist = new ArrayList<>();

public Tab3Tracks(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab3tracks, container, false);


    final String trackid = MediaStore.Audio.Media._ID;
    final String trackno = MediaStore.Audio.Media.TRACK;
    final String trackname = MediaStore.Audio.Media.TITLE;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    ContentResolver cr = getContext().getContentResolver();
    final String[] projection = {trackid,trackno,trackname,path
    };
    final Cursor cursor = cr.query(uri,projection,null,null,null);
    if (cursor!=null){
        if(cursor.moveToFirst()){
            do{
                int trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                trackslist.add(cursor.getString(trackIndex));
            }while(cursor.moveToNext());
        }
    } cursor.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(),R.layout.playlist_item,R.id.songTitle,trackslist
    );
    setListAdapter(adapter);
    return v;
}


public ArrayList<String> getList(){
    return trackslist;
}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    int songIndex = position;

    // Starting new intent
    Intent in = new Intent(getActivity(), NowPlaying.class);
    // Sending songIndex to PlayerActivity
    in.putExtra("songIndex", songIndex);
    getActivity().setResult(100, in);
    getActivity().finish();
    startActivity(in);
}
}

NowPlaying Class:

public class NowPlaying extends AppCompatActivity{

 public ArrayList<String> songsList = new ArrayList<String>();
 public Tab3Tracks tab3;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);
    // Getting all songs list
    tab3 = new Tab3Tracks();
    songsList= tab3.getList();

 }
 }
  

第二个解决方案:

Tab3Tracks类:

public class Tab3Tracks extends ListFragment {

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

public ArrayList<String> trackslist = new ArrayList<>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab3tracks, container, false);


    final String trackid = MediaStore.Audio.Media._ID;
    final String trackno = MediaStore.Audio.Media.TRACK;
    final String trackname = MediaStore.Audio.Media.TITLE;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    ContentResolver cr = getContext().getContentResolver();
    final String[] projection = {trackid,trackno,trackname,path
    };
    final Cursor cursor = cr.query(uri,projection,null,null,null);
    if (cursor!=null){
        if(cursor.moveToFirst()){
            do{
                int trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                trackslist.add(cursor.getString(trackIndex));
            }while(cursor.moveToNext());
        }
    } cursor.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(),R.layout.playlist_item,R.id.songTitle,trackslist
    );
    setListAdapter(adapter);
    return v;
}


public static ArrayList<String> getList(){
    return trackslist;
}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    int songIndex = position;

    // Starting new intent
    Intent in = new Intent(getActivity(), NowPlaying.class);
    // Sending songIndex to PlayerActivity
    in.putExtra("songIndex", songIndex);
    getActivity().setResult(100, in);
    getActivity().finish();
    startActivity(in);
}

NowPlaying Class:

public class NowPlaying extends AppCompatActivity{

 public ArrayList<String> songsList = new ArrayList<String>();

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);
    // Getting all songs list
    songsList= Tab3Tracks.getList();

 }
 }