自定义ExpandableListView在“setAdapter”崩溃

时间:2013-11-06 15:55:44

标签: android call expandablelistview expandablelistadapter expandable

我正在尝试创建自定义的ExpandableListView,但它总是提供 NullPointException 。我已经尝试过调试它,它总是在 myExpandableList.setAdapter(listAdapter)

崩溃

我搜索了其他解决方案,以“ ManagerListActivity.this ”形式发送上下文,“”表单没有帮助。

代码示例:

公共类ManagerListActivity扩展了SherlockActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_list);

    // Get the ExpandableListView
    ExpandableListView managerList = (ExpandableListView) findViewById(R.id.list_manager);
    ManagerListAdapter listAdapter = new ManagerListAdapter(ManagerListActivity.this);

    ArrayList<Subordinate> managers = new ArrayList<Subordinate>();
    ArrayList<Employee> employees = new ArrayList<Employee>();

    ArrayList<String> listDataHeader = new ArrayList<String>();
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();

//我正在跳过创建经理和员工的部分

        for (int i = 0; i < managers.size(); i++) {
            // Name the headers
            listDataHeader.add(managers.get(i).getFirstName());
            List<String> childList = new ArrayList<String>();

            for (int j = 0; j < managers.get(i).getProjects().size(); j++) {
                // Name the children
                childList.add(managers.get(i).getProjects().get(j)
                        .getProjectName());
            }

            listDataChild.put(listDataHeader.get(i), childList);
        }

    listAdapter = new ManagerListAdapter(this, managers, listDataChild,
            employees);
    managerList.setAdapter(listAdapter);
}

现在适配器:

公共类ManagerListAdapter扩展了BaseExpandableListAdapter {

private Context context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<Subordinate> managers;
private ArrayList<Employee> employees;
String phone = "";

public ManagerListAdapter(Context context, ArrayList<Subordinate> managers,
        HashMap<String, List<String>> listDataChild,
        ArrayList<Employee> employees) {
    this.context = context;
    for (int i = 0; i < managers.size(); i++) {
        _listDataHeader.add(managers.get(i).getFirstName());
    }
    this._listDataChild = listDataChild;
    this.employees = employees;
}

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

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_manager_child,
                null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.manager_child);

    txtListChild.setText(childText);
    return convertView;
}


@Override
public View getGroupView(int pos, boolean isExpanded, View view,
        ViewGroup parent) {
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.list_manager_item, null);
    }

    TextView lblListHeader = (TextView) view
            .findViewById(R.id.manager_firstname);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(managers.get(pos).getFirstName());

    // Use the holder to pass data to the call button
    RowViewHolder holder = new RowViewHolder();
    holder.callImg = (ImageView) view.findViewById(R.id.gl_call);
    holder.callImg.setOnClickListener(callListener);
    view.setTag(holder);

    phone = employees.get(pos).getContact();

    return view;
}

// Call button handling
private OnClickListener callListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:" + phone));
            context.startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

protected static class RowViewHolder {
    public ImageView callImg;
}

当然,我还实现了BaseExpandableListAdapter所需的所有其他必需方法。

任何帮助都将不胜感激。

UPDATE:

Logcat提取物:

  

11-06 16:13:46.283:E / AndroidRuntime(924):致命异乎寻常:主要   11-06 16:13:46.283:E / AndroidRuntime(924):java.lang.RuntimeException:无法启动活动ComponentInfo {com.tcs.mobility.uy.rmg / com.tcs.mobility.uy.rmg.ManagerListActivity} :java.lang.NullPointerException   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.os.Handler.dispatchMessage(Handler.java:99)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.os.Looper.loop(Looper.java:137)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread.main(ActivityThread.java:5039)   11-06 16:13:46.283:E / AndroidRuntime(924):at java.lang.reflect.Method.invokeNative(Native Method)   11-06 16:13:46.283:E / AndroidRuntime(924):at java.lang.reflect.Method.invoke(Method.java:511)   11-06 16:13:46.283:E / AndroidRuntime(924):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   11-06 16:13:46.283:E / AndroidRuntime(924):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   11-06 16:13:46.283:E / AndroidRuntime(924):at dalvik.system.NativeStart.main(Native Method)   11-06 16:13:46.283:E / AndroidRuntime(924):引起:java.lang.NullPointerException   11-06 16:13:46.283:E / AndroidRuntime(924):at com.tcs.mobility.uy.rmg.ManagerListAdapter。(ManagerListAdapter.java:63)   11-06 16:13:46.283:E / AndroidRuntime(924):at com.tcs.mobility.uy.rmg.ManagerListActivity.onCreate(ManagerListActivity.java:146)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.Activity.performCreate(Activity.java:5104)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)   11-06 16:13:46.283:E / AndroidRuntime(924):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)   11-06 16:13:46.283:E / AndroidRuntime(924):... 11更多

1 个答案:

答案 0 :(得分:0)

我猜想手机应该包含在View的标签中(准确地说是在RowViewHolder中)?假设onClickListener依赖于来自View的数据更安全。在您的代码中,手机值取决于最后刷新的groupView