我不能为我的生活弄清楚这一点。我只想用我的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
非常感谢任何帮助。
答案 0 :(得分:3)
初始化FoodItems
。你没有。
FoodItems = new ArrayList<HashMap<String, String>>();
BTW,用小写字母启动所有变量名称。
答案 1 :(得分:1)
初始化foodItems,因为它未初始化。所以你尝试添加并获取nullPointer Eception
ArrayList<HashMap<String, String>> FoodItems = new ArrayList<HashMap<String, String>>();
答案 2 :(得分:1)
您尚未初始化 FoodItems
使用:
ArrayList<HashMap<String, String>> FoodItems = new ArrayList<HashMap<String, String>>();