从ExpandableListView调用的片段Communicator接口无法正常工作

时间:2016-03-02 21:00:32

标签: java android android-fragments interface expandablelistview

我可以从子节点中提取数据并向其发送Toast消息。但如果我尝试使用我的Communicator界面,它会立即崩溃。 Communicator适用于我的所有其他片段和片段列表。无论我从ExpandableListView Fragment调用基本的toastMessage方法,它都会不断崩溃。

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

1 个答案:

答案 0 :(得分:1)

我认为每次在Fragment中都有空指针异常,因为你没有初始化和设置

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