Akshay Sharma Akshay Sharma - 4 months ago 11
Android Question

Is it possible to Get ArrayList from Another Class through this method?

I am creating a small music player app. I have two classes named "Tab3Tracks" and "NowPlaying". I created an ArrayList in "Tab3Tracks" class. Now, I want to access the arraylist which is loaded with songs in "NowPlaying" class. Check my code for mistakes. I am getting error in accessing ArrayList.

Tab3Tracks Class

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);
}
}


Is my method correct in accessing ArrayList from Another class ?

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 Runtime Error

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

Answer

You are calling the method getlist() in a wrong way. You can get this from two methods:

1) Making Instance of the class(tab3tracks) in the class you want and then calling the method getlist().

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

2) Making the method static and then call it from another class without making instance of the class(tab3tracks)(which contains method getlist())

Tab3tracks.getlist();

The error that you asked is that you have not made the getlist() static and you are calling it statistically.

First Solution---

Tab3Track Class -:

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();

 }
 }

Second Solution:

Tab3Tracks Class:

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();

 }
 }
Comments