My issue is that i am trying to get the String Array to Shuffle and display the Random List back into the List View. my shuffle is inside my Custom Adapter.
My SongAdapter
public class SongAdapter extends ArrayAdapter<Model> {
public static String[] listSongs = new String[]{}; //String Array of ListSongs#
public void loadFile() {
try {
Resources ResFiles = myContext.getResources(); //ResFile is set to getResources,
InputStream ReadDbFile = ResFiles.openRawResource(R.raw.song); //InputStream is Called to get Text file Songs
BufferedReader reader = new BufferedReader(new InputStreamReader(ReadDbFile));
String DbLines;
while ((DbLines = reader.readLine()) != null)//While DBlines is not Null keep Reading.
{
listSongs = DbLines.split("#");//Dblines is Splitting the Text where # is..
Model SongModel1 = new Model(); //Call the Model Class
SongModel1.setName(listSongs[0]);//Set Name from String Array
SongModel1.setSigner(listSongs[1]);//Set Signer from String Array
SongModel1.setUrl(listSongs[2]); //Set URL from String Array
this.add(SongModel1);//Add SongModel String Array to the Model Class
}
//Catch any Exception..
} catch (IOException e) {
e.printStackTrace();
}
}
public void shuffle(){
Collections.shuffle(Arrays.asList(listSongs));
notifyDataSetChanged();
}
List Fragment
public class MenuFragment extends ListFragment {
static protected SongAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
adapter = new SongAdapter(getActivity(),
android.R.layout.simple_list_item_1);
setListAdapter(adapter);
setHasOptionsMenu(true);
return view;
}
And Finally i am calling in the ActionBar On Click inside Main class
public class MainActivity extends ActionBarActivity {
static protected SongAdapter adapter;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.shuffle:
adapter.shuffle();
return true;
The Error i am getting at Run time is
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.dfoley.project2, PID: 1304
java.lang.NullPointerException
at com.example.dfoley.project2.MainActivity.onOptionsItemSelected(MainActivity.java:135)
at android.app.Activity.onMenuItemSelected(Activity.java:2600)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:551)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)