大家好,我正在尝试制作一个动态可滚动的 selectiondata 列表,用户可以在需要时添加或删除微调器和 editText。我添加了 Spinner 和 editText 没有问题,但是当我尝试删除列表底部的那个时..我收到一个空指针错误。
起初我认为这是因为我没有正确引用它们(相关的editText和微调器数据保存在哈希图中)但是我已经打印出键名并且它们看起来是正确的。
这是代码..
变量
int count = 0;
HashMap<String,Spinner> spinnerMap = new HashMap<String,Spinner>();
HashMap<String,EditText> editTextMap = new HashMap<String,EditText>();
LinearLayout horzLayout;
LinearLayout layout;
addRow 方法
public void addRow()
{
Toast.makeText(getApplicationContext(), "editText"+count, Toast.LENGTH_SHORT).show();
layout = (LinearLayout) findViewById(R.id.scrollerLayout);
LinearLayout horzLayout = new LinearLayout(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
editTextMap.put("editText"+count, new EditText(this));
editTextMap.get("editText"+count).setWidth(100);
editTextMap.get("editText"+count).setHeight(40);
spinnerMap.put("spinner"+count, new Spinner(this));
List<String> spinnerItems = new ArrayList<String>();
spinnerItems.add("Super Duper Bad Evil Weed");
spinnerItems.add("Super Duper Edible Weed");
spinnerItems.add("Funky Looking Weed");
spinnerItems.add("Special Weed");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerItems);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerMap.get("spinner"+count).setAdapter(dataAdapter);
horzLayout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(horzLayout, p);
horzLayout.addView(spinnerMap.get("spinner"+count));
horzLayout.addView(editTextMap.get("editText"+count));
count++;
}
RemoveRow 方法
public void removeRow()
{
count--;
Toast.makeText(getApplicationContext(), "editText"+count, Toast.LENGTH_SHORT).show();
horzLayout.removeView(spinnerMap.get("spinner"+count));
spinnerMap.remove("spinner"+count);
horzLayout.removeView(editTextMap.get("editText"+count));
editTextMap.remove("editText"+count);
}
确切的错误消息..
> 06-09 23:48:33.369: E/AndroidRuntime(560): FATAL EXCEPTION: main
06-09 23:48:33.369: E/AndroidRuntime(560): java.lang.NullPointerException
06-09 23:48:33.369: E/AndroidRuntime(560): at org.unisa.paddockpad.ScreenTwo.removeRow(ScreenTwo.java:83)
06-09 23:48:33.369: E/AndroidRuntime(560): at org.unisa.paddockpad.ScreenTwo$2.onClick(ScreenTwo.java:52)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.view.View.performClick(View.java:3511)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.view.View$PerformClick.run(View.java:14105)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Handler.handleCallback(Handler.java:605)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Looper.loop(Looper.java:137)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-09 23:48:33.369: E/AndroidRuntime(560): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 23:48:33.369: E/AndroidRuntime(560): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 23:48:33.369: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-09 23:48:33.369: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-09 23:48:33.369: E/AndroidRuntime(560): at dalvik.system.NativeStart.main(Native Method)
任何帮助将不胜感激!
更新
在该方法中创建视图后,我可以立即删除它们。