2014-12-03 43 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()並在按下菜單的按鈕時調用此方法