从返回的hashmap获取列表

时间:2015-08-26 21:07:03

标签: java android list hashmap

我有一个名为prepareListData的方法。此方法准备数据以填充Android中的ExpandableListView。当方法放在调用它的类中时,它完美地工作,并且在使用私有字段时不需要返回任何内容。当我将方法移动到它自己的类并使其成为静态时,我得到一个空指针,因为它显然需要返回它准备的数据。

我创建的类具有相同的方法(prepareListData),返回HashMap<String, List<String>>。我试图从其他类中调用该方法,我希望将返回的List<>中的HashMap分配给List<String>变量。有没有办法在Java中实现这个目标?

我之前从未尝试过,所以我不知道从哪里开始。

这是我的带有prepareListData方法的类:

public class PrepareListData {

    public static HashMap prepareListData() {
        List<String> gpsMenuListDataHeader = new ArrayList<>();
        HashMap<String, List<String>> gpsMenuListDataChild = new HashMap<>();

        // Main group data
        gpsMenuListDataHeader.add("Job Card");
        gpsMenuListDataHeader.add("Sign Off");
        gpsMenuListDataHeader.add("Test Installation");

        // Child Items for Job Card
        List<String> jobcard = new ArrayList<>();
        jobcard.add("Job Card Sub Item 1");
        jobcard.add("Job Card Sub Item 2");
        jobcard.add("Job Card Sub Item 3");
        jobcard.add("Job Card Sub Item 4");
        jobcard.add("Job Card Sub Item 5");

        // Child Items fos Sign Off
        List<String> signoff = new ArrayList<>();
        signoff.add("Sign Off Sub Item 1");
        signoff.add("Sign Off Sub Item 2");
        signoff.add("Sign Off Sub Item 3");

        // Child Items for Test Installation
        List<String> testInstallation = new ArrayList<>();
        testInstallation.add("Test Installation Sub Item 1");
        testInstallation.add("Test Installation Sub Item 2");
        testInstallation.add("Test Installation Sub Item 3");
        testInstallation.add("Test Installation Sub Item 4");
        testInstallation.add("Test Installation Sub Item 5");
        testInstallation.add("Test Installation Sub Item 6");
        testInstallation.add("Test Installation Sub Item 7");

        gpsMenuListDataChild.put(gpsMenuListDataHeader.get(0), jobcard);
        gpsMenuListDataChild.put(gpsMenuListDataHeader.get(1), signoff);
        gpsMenuListDataChild.put(gpsMenuListDataHeader.get(2), testInstallation);

        return gpsMenuListDataChild;
    }
}

以下是我尝试调用并提取List&lt;&gt;的方法的片段。来自HashMap:

gpsMenuListView = (ExpandableListView) findViewById(R.id.gps_menu_drawer);
prepareListData();
gpsMenuListDataHeader = PrepareListData.prepareListData().;
gpsMenuListAdapter = new ExpandableListAdapter(this, gpsMenuListDataHeader, gpsMenuListDataChild);
gpsMenuListView.setAdapter(gpsMenuListAdapter);

堆栈跟踪:

08-26 23:12:25.031  32157-32164/za.co.gpsts.gpsjobcard E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe
08-26 23:12:25.039  32157-32157/za.co.gpsts.gpsjobcard E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: za.co.gpsts.gpsjobcard, PID: 32157 
java.lang.RuntimeException: Unable to start activity ComponentInfo{za.co.gpsts.gpsjobcard/za.co.gpsts.gpsjobcard.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at za.co.gpsts.gpsjobcard.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:45)
    at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
    at android.widget.ListView.setAdapter(ListView.java:487)
    at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:603)
    at za.co.gpsts.gpsjobcard.MainActivity.onCreate(MainActivity.java:50)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案