0

我可以將數據拉出孩子和吐司消息。但是如果我嘗試使用我的Communicator接口,它會立即崩潰。 Communicator對我所有其他片段和片段列表都可以正常工作。無論我從哪裏調用ExpandableListView片段的基本toastMessage方法,它都會不斷崩潰。片段Communicator接口調用ExpandableListView不起作用

/** 
* Created by petakirikiri on 3/03/16. 
*/ 
public class ThingsInputFragmentExpandableList extends Fragment { 
View v; 
HashMap<String, List<String>> movies_category; 
List<String> movies_list; 
ExpandableListView Exp_list; 
MoviesAdapter adapter; 
Communicator comm; 

public ThingsInputFragmentExpandableList() { 

} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 


    v = inflater.inflate(R.layout.movies_expandablelist_fragment, container, false); 
    return v; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    super.onCreate(savedInstanceState); 

    Exp_list = (ExpandableListView) v.findViewById(R.id.exp_list); 
    movies_category = ThingsDataProvider.getInfo(); 
    movies_list = new ArrayList<String>(movies_category.keySet()); 
    adapter = new MoviesAdapter(getActivity(), movies_category, movies_list); 
    Exp_list.setAdapter(adapter); 
    Exp_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getActivity(), movies_list.get(groupPosition) + " is expanded", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    Exp_list.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 
     @Override 
     public void onGroupCollapse(int groupPosition) { 
      Toast.makeText(getActivity(), movies_list.get(groupPosition) + " is collapsed", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    Exp_list.setOnChildClickListener(new  ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      if (movies_list.get(groupPosition) == "Determiners"){ 
       comm.toastMessageTest(); 
      } 
      else if (movies_list.get(groupPosition) == "Things"){ 


      } 
      else if (movies_list.get(groupPosition) == "Adjectives"){ 


      } 

      return false; 
     } 
    }); 
} 
} 

public interface Communicator { 
public void toastMessageTest(); 
public void setIntransitiveFragment(); 
public void setTransitiveFragment(); 
public void setPronounFragment(); 
public void setThingFragment(); 
public void setLocationFragment(); 
public void setVerb(String data); 
public void setTense(String data); 
public void setSubject(String data); 
public void setDeterminerSubject(String data); 
public void setThingSubject(String data); 
public void setAdjectiveSubject(String data); 
} 

public class MainActivity extends Activity implements Communicator{ 

@Override 
public void toastMessageTest() { 
    Toast.makeText(this, "This works", Toast.LENGTH_LONG).show(); 
} 
} 
+0

這與您的問題無關,但我相信,您應避免使用ExpandableListView,它已過時並且不屬於最新的設計指南。 –

回答

1

我覺得你有空指針異常的片段,每次因爲你沒有初始化並設置

Communicator comm; 

你需要的是在片段覆蓋onAttach 例子:

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    if (context instanceof Communicator) 
     conn = (Communicator)context; 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    if (activity instanceof Communicator) 
     conn= (Communicator)activity; 
} 
+0

你先生,是我的英雄。非常感謝您爲我檢查這個過程,非常感謝您的幫助。 –