java - 自定义 ListAdapter 的 Hashmap 中出现空指针异常?

标签 java android listview nullpointerexception

我一生都无法弄清楚这一点。我只想使用我的 Hashmap 将一些自定义数据添加到 ListView 中。

这个过程是我将信息放入 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

任何帮助将不胜感激。

最佳答案

初始化FoodItems。你没有。

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

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

关于java - 自定义 ListAdapter 的 Hashmap 中出现空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616460/

相关文章:

android - react native : JAVA_HOME is not set and no 'java' command could be found in your PATH

android - 将多个 LayerList 添加到 API 19 设备导致充气错误

android - Volley正在获取数据,但有时会在listview中显示数据,有时不会

android - 设置Android中ListView的 'empty'区域的颜色

java - 尝试从服务器获取响应,但仅显示 "Toast Server not responding"

java - 以交互方式从 java 代码调用 perl 脚本

Java 8 在非静态方法中映射内部静态类属性

java - Selenium 脚本因 TestNG 注释而失败

java - Facebook 集成 android 支持 v4 内容 localBroadcastManager

Android - 滚动时 ListView 图像随机播放