为什么ExpandableListView在设置子视图数据时会给出NullPointerException?

问题描述 投票:0回答:1

我正在创建ExpandableListView,其中我将从json添加数据。所以首先我添加组名并在ExpandableListView中显示它工作正常但是当我将子数据添加到ExpandableListView时它崩溃了。

这里我有2个网址。一个用于组列表,另一个用于数据。

现在点击任何组我调用第二个url传递被点击的组名但它给出了NullPointerException。

如何解决这个问题

这是错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
                                                                                        at grepthorsoft.com.grepthorapplication.adapters.AccountDetailsAdapter1.getChildrenCount(AccountDetailsAdapter1.java:51)
                                                                                        at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
                                                                                        at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
                                                                                        at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:693)
                                                                                        at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:653)
                                                                                        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3133)
                                                                                        at android.widget.AbsListView$3.run(AbsListView.java:4048)
                                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我的代码

account_details_list1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="grepthorsoft.com.grepthorapplication.activities.AccountDetailsList1">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Account Details"
        android:padding="10dp"
        android:textSize="20dp"
        android:textStyle="bold"
        android:gravity="center_horizontal"
        android:id="@+id/account_details_list1_heading"/>


<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/account_details_expand"
    android:layout_below="@+id/account_details_list1_heading"/>

</RelativeLayout>

account details list1.Java

public class AccountDetailsList1 extends AppCompatActivity {
ExpandableListView expand;
    List<String> group;
    List<AccountDetailsRow1> child;
    HashMap<String,List<AccountDetailsRow1>> child_map;
    AccountDetailsAdapter1 expandAdap;
    AccountDetailsAdapter2 expandAdap1;
JSONArray account_details_jsonArray1,account_details_result_jsonArray2,get_groups_jsonArray1,get_groups_jsonArray2;
JSONObject account_details_jsonObject1,account_details_result_jsonArray2_i,get_groups_jsonObject1,get_groups_jsonObject2;
String account_details_status,account_details_result,get_groups_status,get_groups_result;
String[] account_details_group_name,account_details_card_holder_code,account_details_name,account_details_contactnumber,account_details_email_id,get_groups_group_name;






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




        expand=findViewById(R.id.account_details_expand);
        group=new ArrayList<>();
        child=new ArrayList<>();
        child_map=new HashMap<>();

getGroups();



expand.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

        getDetails(i,(String) expandableListView.getItemAtPosition(i));

    return false;

    }
});



 public void getGroups()
    {
        RequestQueue rq = Volley.newRequestQueue(AccountDetailsList1.this);
        StringRequest stringRequest = new StringRequest( Request.Method.GET,
                "http://grepthorsoftware.in/tst/greppayment/webservices/getgroups.php",




                new Response.Listener<String>() {
                    public void onResponse(String response) {



                        try {


                            System.out.println("jhfgf:"+response);
                            get_groups_jsonArray1=new JSONArray(response);
                            get_groups_jsonObject1= get_groups_jsonArray1.getJSONObject(0);
                            System.out.println("sdfgsgsg:"+get_groups_jsonObject1);
                            get_groups_status= get_groups_jsonObject1.getString("status");
                            System.out.println("statsae5656:"+get_groups_status);

                            if(  get_groups_status.equals("1")) {
                                get_groups_result = get_groups_jsonObject1.getString("result");
                                System.out.println("result94:" + get_groups_result);
                                get_groups_jsonArray2 = new JSONArray(get_groups_result);
                                System.out.println("result454:" + get_groups_jsonArray2);
                                System.out.println("result454898989:" + get_groups_jsonArray2.length());


                                account_details_group_name = new String[get_groups_jsonArray2.length()];


                                if (account_details_group_name.length > 0) {
                                    account_details_group_name = null;

                                    account_details_group_name = new String[get_groups_jsonArray2.length()];

                                }

                                System.out.println("result567655:" + account_details_group_name.length);


                                if (group.size() > 0) {
                                    group.clear();
                                }


                                for (int i = 0; i < get_groups_jsonArray2.length(); i++) {
                                    get_groups_jsonObject2 = get_groups_jsonArray2.getJSONObject(i);
                                    account_details_group_name[i] = get_groups_jsonObject2.getString("GROUP_NAME");
                                    group.add(account_details_group_name[i]);

                                }
                                for (int i = 0; i < group.size(); i++) {
                                    System.out.println("kkk:" + group.get(i));
                                }
                                expandAdap=new AccountDetailsAdapter1(AccountDetailsList1.this,group);
                                expand.setAdapter(expandAdap);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    } },





                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        // TODO Auto-generated method stub

                    }
                })



        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();




                return params;
            }
        };


        rq.add(stringRequest);





    }






















public void getDetails(final int getSelectedItemPosition, final String getItemAtPosition)
{

    System.out.println("getPItem:"+ getSelectedItemPosition);
    System.out.println("getPItem3565:"+getItemAtPosition);
    RequestQueue rq = Volley.newRequestQueue(AccountDetailsList1.this);
    StringRequest stringRequest = new StringRequest( Request.Method.POST,
            "http://grepthorsoftware.in/tst/greppayment/webservices/getdata.php",


            new Response.Listener<String>() {
                public void onResponse(String response) {
                    try {

System.out.println("asdfg:"+response);
                        account_details_jsonArray1=new JSONArray(response);

                        account_details_jsonObject1 = account_details_jsonArray1.getJSONObject(0);
                        account_details_status= account_details_jsonObject1.getString("status");


                        if(account_details_status.equals("1")) {
                            account_details_result = account_details_jsonObject1.getString("result");
                            account_details_result_jsonArray2 = new JSONArray(account_details_result);

 account_details_card_holder_code = new String[account_details_result_jsonArray2.length()];
                            account_details_name = new String[account_details_result_jsonArray2.length()];
                            account_details_contactnumber = new String[account_details_result_jsonArray2.length()];
                            account_details_email_id = new String[account_details_result_jsonArray2.length()];




                            if (account_details_group_name.length > 0 || account_details_card_holder_code.length > 0 || account_details_name.length > 0 || account_details_contactnumber.length > 0 || account_details_email_id.length > 0)
                            {

account_details_card_holder_code = null;
                                account_details_name = null;
                                account_details_contactnumber = null;
                                account_details_email_id = null;

      account_details_card_holder_code = new String[account_details_result_jsonArray2.length()];
                                account_details_name = new String[account_details_result_jsonArray2.length()];
                                account_details_contactnumber = new String[account_details_result_jsonArray2.length()];
                                account_details_email_id = new String[account_details_result_jsonArray2.length()];

                            }


 for(int i=0;i<account_details_result_jsonArray2.length();i++)
                            {

                                account_details_result_jsonArray2_i=account_details_result_jsonArray2.getJSONObject(i);

 account_details_card_holder_code[i] = account_details_result_jsonArray2_i.getString("CARD_HOLDER_CODE");
                                account_details_name[i] = account_details_result_jsonArray2_i.getString("NAME");
                                account_details_contactnumber[i] = account_details_result_jsonArray2_i.getString("CONTACT_NUMBER");
                                account_details_email_id[i] = account_details_result_jsonArray2_i.getString("EMAIL_ID");



                            }




                            if (group.size() > 0) {
                                group.clear();
                            }

                            for(int i=0;i<account_details_group_name.length;i++) {
                            group.add(account_details_group_name[i]);
                            }






                            if (child.size() > 0) {
                                child.clear();
                            }

                            for(int i=0;i<account_details_card_holder_code.length || i<account_details_name.length || i<account_details_contactnumber.length || i<account_details_email_id.length;i++) {
                                child.add(new AccountDetailsRow1(account_details_card_holder_code[i],account_details_name[i],account_details_contactnumber[i],account_details_email_id[i]));
                            }









System.out.println("anjali:"+group.get(getSelectedItemPosition));





                            child_map.put(group.get(getSelectedItemPosition),child);


                            expandAdap=new AccountDetailsAdapter1(AccountDetailsList1.this,group,child_map);
                            expand.setAdapter(expandAdap);







                        }
                    }catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                ;
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            // TODO Auto-generated method stub

        }
    })

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();

            params.put("groupname",getItemAtPosition);

            return params;
        }
    };
    rq.add(stringRequest);
}

}

account details adapter1.Java

public class AccountDetailsAdapter1 extends BaseExpandableListAdapter {

    Context context;
    List<String> group;
    HashMap<String,List<AccountDetailsRow1>> child_map;


    public AccountDetailsAdapter1(Context context, List<String> group,  HashMap<String,List<AccountDetailsRow1>> child_map) {
        this.context = context;
        this.group = group;
        this.child_map = child_map;
    }

    public AccountDetailsAdapter1(Context context, List<String> group) {
        this.context = context;
        this.group = group;

    }

    //count

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

    @Override
    public int getChildrenCount(int i) {
        return child_map.get(group.get(i)).size();
    }






    //groups

    @Override
    public Object getGroup(int i) {
        return group.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {

        return child_map.get(group.get(i)).get(i1);
    }


    //id

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }


    //extras

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

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


    //inflating views

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String group = (String) getGroup(i);

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

        TextView t = view.findViewById(R.id.account_details_expandable_header);
        t.setText(group);


        /*RelativeLayout l=view.findViewById(R.id.account_details_expandable_header_layout);
        if(b)
        {
            l.setBackgroundColor(Color.parseColor("#000000"));
        }*/
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

        AccountDetailsRow1 child = (AccountDetailsRow1) getChild(i, i1);

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




        TextView t1 = view.findViewById(R.id.account_details_card_holder_code_row);
        TextView t2 = view.findViewById(R.id.account_details_name_row);
        TextView t3 = view.findViewById(R.id.account_details_contactnumber_row);
        TextView t4 = view.findViewById(R.id.account_details_email_id_row);



        t1.setText(child.getAccountDetailsCardHolderCode());
        t2.setText(child.getAccountDetailsName());
        t3.setText(child.getAccountDetailsContactNumber());
        t4.setText(child.getAccountDetailsEmailId());



        return view;
    }


}

account_details_expandable_header.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/account_details_expandable_header_layout">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/account_details_expandable_header"
        android:text="A"
        android:textSize="15dp"
        android:gravity="center_vertical"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        />


</RelativeLayout>

account_details_expandable_row.xml

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.v7.widget.CardView

        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="3dp"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ECEFF1">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Card Holder Code"
                    android:layout_marginStart="15dp"
                    android:id="@+id/account_details_card_holder_code_row"
                    android:textSize="15dp"
                    android:textStyle="bold"/>



            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ankit"
                    android:layout_marginStart="15dp"
                    android:id="@+id/account_details_name_row"/>
            </RelativeLayout>


            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="726532"
                    android:layout_marginStart="15dp"
                    android:id="@+id/account_details_contactnumber_row"/>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="[email protected]"
                    android:layout_marginStart="15dp"
                    android:id="@+id/account_details_email_id_row"/>


            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
android expandablelistview
1个回答
0
投票

好的,所以看起来你的代码崩溃了,因为这个方法返回null。我添加了一个空检查。看看它是否有效:

    @Override
    public int getChildrenCount(int i) {
        if (child_map.get(group.get(i)) != null) {
            return child_map.get(group.get(i)).size();
        }
        return 0;
    }
© www.soinside.com 2019 - 2024. All rights reserved.