0

我这辈子都想不通。我只想用我的 Hashmap 将一些自定义数据添加到列表视图中。

过程是我将信息放入 Hashmap,然后将 hashmap 放入 ArrayList - 这会导致 NullPointerException,我不知道如何解决此问题。

public class RecipeDownload extends Activity {
    // Declare Variables
    ArrayList<HashMap<String, String>> FoodItems;
    public static String TitleofFood = "TitleofFood";
    public static String LinktoRecipe = "LinktoRecipe";
    public static String IngredientsofRecipe = "IngredientsofRecipe";
    public static String ThumbnailofFood = "thumbnail";


    public HashMap<String, String> RecipeItems = new HashMap<String, String>();


    EditText SearchField;
    Button ResearchButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.recipedownload);

        // Declaration of Button "Refreshbutton".
        ResearchButton = (Button) findViewById(R.id.RefreshButton);

        // Declaration of EditText "SearchField"
        SearchField = (EditText) findViewById(R.id.SearchField);

        SearchField.setSelected(false);

        SearchField.setText(MainActivity.ItemToSearch);

        // Execute ConnectionCheck
        CheckConnection();

        // Recipe List Context Menu
        //registerForContextMenu(RecipeList);   


        RecipeItems.put(TitleofFood, "href");
        RecipeItems.put(LinktoRecipe, "href");
        RecipeItems.put(IngredientsofRecipe, "ingredients");
        RecipeItems.put(ThumbnailofFood, "thumbnail");

        FoodItems.add(RecipeItems);


    }

这会在第 71 行创建一个 NullPointerException。

09-04 16:16:46.589: E/AndroidRuntime(10372): FATAL EXCEPTION: main
09-04 16:16:46.589: E/AndroidRuntime(10372): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.paul.barcoder/org.paul.barcoder.RecipeDownload}: java.lang.NullPointerException
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.os.Looper.loop(Looper.java:137)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at java.lang.reflect.Method.invoke(Method.java:511)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at dalvik.system.NativeStart.main(Native Method)
09-04 16:16:46.589: E/AndroidRuntime(10372): Caused by: java.lang.NullPointerException
09-04 16:16:46.589: E/AndroidRuntime(10372):    at org.paul.barcoder.RecipeDownload.onCreate(RecipeDownload.java:71)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.Activity.performCreate(Activity.java:5206)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
09-04 16:16:46.589: E/AndroidRuntime(10372):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
09-04 16:16:46.589: E/AndroidRuntime(10372):    ... 11 more

任何帮助将不胜感激。

4

3 回答 3

3

初始化FoodItems. 你没有。

 FoodItems = new ArrayList<HashMap<String, String>>();

顺便说一句,所有变量名都以小写字母开头。

于 2013-09-04T14:24:42.733 回答
1

初始化 foodItems 因为它不是 Initialized 。所以你尝试添加并获取 nullPointer Eception

ArrayList<HashMap<String, String>> FoodItems = new ArrayList<HashMap<String, String>>();
于 2013-09-04T14:26:08.240 回答
1

你还没有初始化FoodItems

利用 :

 ArrayList<HashMap<String, String>> FoodItems = new ArrayList<HashMap<String, String>>();
于 2013-09-04T14:26:30.107 回答