2016-11-09 49 views
0

我正在製作一個帶有多個片段的選項卡布局,這些片段需要將一個對象從之前的活動傳遞給它們,以便它們可以填充它們的列表,並且當我試圖從束。我之前已經通過其他活動之間沒有問題的同一個對象。我創建了對象Parcelable,這樣我就可以將它添加到一個包中,並將它作爲參數傳遞。下面是該吐出的標籤佈局中使用的片段適配器類代碼:傳遞給Fragment的包是否爲空?

package edu.uml.android.adventurersarchive; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

import edu.uml.android.adventurersarchive.character.CharacterInfo; 

public class SpellbookTabAdapter extends FragmentPagerAdapter { 
    private Context mContext; 
    private CharacterInfo myCharacter; 

    public SpellbookTabAdapter(Context context, CharacterInfo ch, FragmentManager fm) { 
     super(fm); 
     mContext = context; 
     myCharacter = ch; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Bundle bundle = new Bundle(); 
     if(position == 0) { 
      PreparedSpellsFragment prepared = new PreparedSpellsFragment(); 
      bundle.putParcelable("character", myCharacter); 
      prepared.setArguments(bundle); 
      return prepared; 
     } else if(position == 1) { 
      MySpellbookFragment myspellbook = new MySpellbookFragment(); 
      bundle.putParcelable("character", myCharacter); 
      myspellbook.setArguments(bundle); 
      return myspellbook; 
     } else if(position == 2) return new FullSpellbookFragment(); 
     else return null; 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     if(position == 0) return "Prepared"; 
     else if(position == 1) return "My Spells"; 
     else if(position == 2) return "All Spells"; 
     else return ""; 
    } 
} 

這裏是PreparedSpellsFragment類。該代碼是在MySpellbookFragment類幾乎相同(使用的片段是如何與對象進行交互除外):

package edu.uml.android.adventurersarchive; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 

import java.util.List; 
import java.util.Map; 

import edu.uml.android.adventurersarchive.character.CharacterInfo; 
import edu.uml.android.adventurersarchive.info.Spell; 

public class PreparedSpellsFragment extends Fragment { 
    private SpellListAdapter adapter; 
    private List<String> groupHeaders; 
    private Map<String, List<Spell>> groupItems; 

    public PreparedSpellsFragment() { 
     Bundle bundle = getArguments(); 
     CharacterInfo myCharacter = (CharacterInfo) bundle.getParcelable("character"); 
     prepareGroups(myCharacter); 
    } 

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

     adapter = new SpellListAdapter(groupHeaders, groupItems); 
     ExpandableListView expView = (ExpandableListView) rootView.findViewById(R.id.spell_list); 
     expView.setAdapter(adapter); 

     expView.setOnChildClickListener(new OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
       // TODO: Launch activity to show detailed information about the spell. 
       return false; 
      } 
     }); 

     return rootView; 
    } 

    private void prepareGroups(CharacterInfo myCharacter) { 
     // TODO: Take spells in player's prepared list and populate the groups and their items. 
     if(myCharacter != null) { 

     } 
    } 
} 

我得到一個錯誤,指出在PreparedSpellsFragment構造函數,我調用「getParcelable() 「方法在一個空的對象引用,暗示從」getArguments()「檢索到的Bundle對象爲空,但我看不到任何理由,捆綁本身爲null,可能是一個OutOfMemoryException,它不能分配空間爲對象。我可以理解,如果對象可能沒有被正確傳遞,或者如果在表示對象的鍵的字符串中存在拼寫錯誤,但是沒有......它表示Bundle對象本身爲空。

任何人都可以找出原因是什麼?

+0

碎片不應該有構造函數 –

+0

你可能會覺得這篇文章有用。 http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment –

回答

0

您正在構造函數中檢查Bundle。取出構造函數,然後檢查片段的onCreateView()中的Bundle。

+0

我剛剛意識到這是我在最後一個項目中做到了這一點,我使用了一個標籤佈局。我是一個愚蠢的男人。謝謝。 –

0
Bundle bundle = getArguments(); 
CharacterInfo myCharacter = (CharacterInfo) bundle.getParcelable("character"); 
prepareGroups(myCharacter); 

將此代碼寫入onCreateView()並檢查。

相關問題