通过在Fragment中调用SetAdapter来实现NullPointerException

时间:2013-11-21 13:33:15

标签: android listview set fragment adapter

以下代码抛出NPE。 我只是找不到解决方案。我从适配器调用void为我的列表视图设置另一个适配器。当我在onCreateView中调用setAdapter时,一切正常。

如果您可以查看我的代码,那会很好。

片段:

public class OrderSettingsFragment extends SherlockFragment {

    //ListViews
    private ListView mChannelOrder;
    private ChannelOrderAdapter mChannelOrderAdapter;
    private ExpandableListView mAvailableChannels;
    private AvailableChannelsAdapter mAvailableChannelsAdapter;
    private ArrayList<Object> lvChildObject = new ArrayList<Object>();
    private ArrayList<String> lvChanneltxt = new ArrayList<String>();
    private byte[][] lvChannelID = new byte[10][2];
    private int ChannelOrderLength = 0;


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.e("ONCRATEVIEW","ORDERSETTINGS");
    final View root = inflater.inflate(R.layout.order_settings,container,false);
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    root.setBackgroundColor(Color.WHITE);

    lvChanneltxt.add("Test");

    mAvailableChannels = (ExpandableListView)root.findViewById(R.id.lv_availaibleChannels);

    setChildData();

    ChannelOrderLength=0;

    mChannelOrder = (ListView)root.findViewById(R.id.lv_channel_order);
    mChannelOrderAdapter = new ChannelOrderAdapter(getActivity(), lvChanneltxt);
    mChannelOrder.setAdapter(mChannelOrderAdapter);


    return root;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initLV();

    }

    private void initLV() {
    }

    private void setChildData() {
        Log.e("CAlle", "Test");

        ArrayList<String> ChildList = new ArrayList<String>();
        ArrayList<String> ChildList0 = new ArrayList<String>();
        ArrayList<String> ChildList1 = new ArrayList<String>();
        ArrayList<String> ChildList2 = new ArrayList<String>();
        ArrayList<String> ChildList3 = new ArrayList<String>();
        ArrayList<String> ChildList4 = new ArrayList<String>();
        ArrayList<String> ChildList5 = new ArrayList<String>();

        lvChildObject.clear();
        ChildList0.clear();
        ChildList1.clear();
        ChildList2.clear();
        ChildList3.clear();
        ChildList4.clear();
        ChildList5.clear();


        ChildList = PlottingFragment.getSignalText();

        String[] Channels = ChildList.toArray(new String[0]);

        for(int i = 0; i < Channels.length; i++ ) {
            int ModuleID = PlottingFragment.getModulID().get(i);

            switch (ModuleID) {
            case 0:
                ChildList0.add(Channels[i]);
                break;
            case 1:
                ChildList1.add(Channels[i]);
                break;
            case 2:
                ChildList2.add(Channels[i]);
                break;
            case 3:
                ChildList3.add(Channels[i]);
                break;
            case 4:
                ChildList4.add(Channels[i]);
                break;
            case 5:
                ChildList5.add(Channels[i]);
                break;
            }

        }



        lvChildObject.add(ChildList0);
        lvChildObject.add(ChildList1);
        lvChildObject.add(ChildList2);
        lvChildObject.add(ChildList3);
        lvChildObject.add(ChildList4);
        lvChildObject.add(ChildList5);

        mAvailableChannelsAdapter = new AvailableChannelsAdapter(getActivity(), PlottingFragment.getModulText() ,
                lvChildObject,
                PlottingFragment.getModuleID(),
                PlottingFragment.getChannelID());
        mAvailableChannels.setAdapter(mAvailableChannelsAdapter);
    }

    public void addChannel(String Channeltxt, byte Module ,byte Channel)  {
        byte[] ModuleChannelMessage = new byte[2];
        ModuleChannelMessage[0] = Module;
        ModuleChannelMessage[1] = Channel;



lvChanneltxt.add(Channeltxt);
    lvChannelID[ChannelOrderLength] = ModuleChannelMessage;
    ChannelOrderLength++;

    mChannelOrder.setAdapter(mChannelOrderAdapter);

}

}

适配器1:

public class AvailableChannelsAdapter extends BaseExpandableListAdapter{

private ArrayList<Object> childtems;
private ArrayList<String> parentItems, child;
private byte[] ModuleID, ChannelID;
private Context context;
private OrderSettingsFragment mOrderSettingsFragment;

public AvailableChannelsAdapter(Context context, ArrayList<String> parents, ArrayList<Object> childern, byte[] ModuleID, byte[] ChannelID) {
    this.parentItems = parents;
    this.childtems = childern;
    this.ModuleID = ModuleID;
    this.ChannelID = ChannelID;
    this.context = context;

    mOrderSettingsFragment = new OrderSettingsFragment();


}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


    child = (ArrayList<String>) childtems.get(groupPosition);

    TextView textView = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.available_channel_drawer, null);
    }

    textView = (TextView) convertView.findViewById(R.id.acd_channeltxt);
    textView.setText(child.get(childPosition));


    Button add = (Button) convertView.findViewById(R.id.acd_plus);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

                mOrderSettingsFragment.addChannel(child.get(childPosition), ModuleID[childPosition], ChannelID[childPosition]);
                Toast.makeText(context, child.get(childPosition), Toast.LENGTH_SHORT ).show();
        }
    });

    return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.available_channel_drawer_parent, null);
    }

    TextView textView = (TextView) convertView.findViewById(R.id.cod_channels_txt);
    textView.setText(parentItems.get(groupPosition));

    return convertView;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) childtems.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

@Override
public int getGroupCount() {
    return parentItems.size();
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}
}

适配器2:

class ChannelOrderAdapter extends BaseAdapter {

private Context context;
private ArrayList<String> Channel;

public ChannelOrderAdapter(Context context, ArrayList<String> Channel) {
    this.context = context;
    this.Channel = Channel;
}

@Override
public int getCount() {
    return Channel.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.channel_order_drawer, null);

    TextView tvChannel = (TextView)convertView.findViewById(R.id.cod_channels_txt);
    tvChannel.setText(Channel.get(position));

    return convertView;
}

}

谢谢!

NPE位于以下行

public void addChannel(String Channeltxt, byte Module ,byte Channel)  {
    byte[] ModuleChannelMessage = new byte[2];
    ModuleChannelMessage[0] = Module;
    ModuleChannelMessage[1] = Channel;

    lvChanneltxt.add(Channeltxt);
    lvChannelID[ChannelOrderLength] = ModuleChannelMessage;
    ChannelOrderLength++;

    mChannelOrder.setAdapter(mChannelOrderAdapter);

}

编辑:LogChat:

03-03 06:36:11.951: E/AndroidRuntime(11613): FATAL EXCEPTION: main
03-03 06:36:11.951: E/AndroidRuntime(11613): java.lang.NullPointerException
03-03 06:36:11.951: E/AndroidRuntime(11613):    at de.edag.ibob.OrderSettingsFragment.addChannel(OrderSettingsFragment.java:140)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at de.edag.ibob.AvailableChannelsAdapter$1.onClick(AvailableChannelsAdapter.java:59)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.view.View.performClick(View.java:4232)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.view.View$PerformClick.run(View.java:17318)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.os.Handler.handleCallback(Handler.java:615)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.os.Looper.loop(Looper.java:137)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at java.lang.reflect.Method.invoke(Method.java:511)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-03 06:36:11.951: E/AndroidRuntime(11613):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案