2014-12-03 58 views
0

我的问题是,我试图让字符串数组随机随机并显示随机列表回到列表视图。我的洗牌是在我的自定义适配器。Shuffling一个自定义适配器

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

名单片段

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

,最后我在动作条我呼吁单击主类

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; 

的错误我得到在运行时正在里面是

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) 
+0

适配器为空 – m0skit0 2014-12-03 20:30:13

+0

可能重复[什么是空指针异常,以及如何修复它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon 2014-12-03 20:36:10

回答

0

您应该使用MenuFragment.adapter.shuffle()因为你adapterMainActivity是从其他的一个(这里面你MenuFragment

作为建议不同,你应该创建一个调用adapter.shuffle()MenuFragment一个公共方法,并调用它从您的MainActivity

+0

所以我应该采取该方法出来的Adpater然后将它放入MenuFragment中,然后调用MainActivity – Dave 2014-12-03 20:38:38

+0

不一定,您可以在'MenuFragment'上创建该方法(称为methodA),并在该方法内从adapter调用'adapter.shuffle()'。最后,从你的'MainActivity'调用你的'fragment.methodA' – DavidGSola 2014-12-03 20:42:00

+0

你好我添加'adapter.shuffle'到'MenuFragment',然后在MainActivity中调用MethodA,但仍然显示空指针,但它阻止应用程序崩溃。 – Dave 2014-12-03 21:55:07

0

Fragment中的与Activity中的不同,只有Fragment中的那个被初始化。你可以做的是在你的片段中定义一个方法,调用array.shuffle()并在按下菜单的按钮时调用此方法