我已经看到了这些问题并尝试了所有答案。谁能告诉我这里我做错了什么?
我将所有声明放在最上面,然后我有了我的arrayadapter,然后我有了onClick的输入按钮,但是当我点击按钮时我尝试输入任何内容时它会崩溃。
代码:
//create a list
final ListView shotList = (ListView) findViewById(R.id.shotList);
String[] values = new String[] { "6/10/13 (Attack)", "4/25/13 (Dr. Apt.)", "3/4/13 (Attack)" };
Button enterButton = (Button)findViewById(R.id.enterButt);
final EditText editText = (EditText)findViewById(R.id.enterText);
final String input = editText.getText().toString();
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
shotList.setAdapter(adapter);
//add data to list via enter button
enterButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.add(input);
adapter.notifyDataSetChanged();
//list.add(new String(editText.getText().toString()));
}
});
////////////////////////////////////////////////////////////////////////////////////
//array adapter
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
logcat的:
06-28 19:24:52.149: D/AndroidRuntime(29515): Shutting down VM
06-28 19:24:52.149: W/dalvikvm(29515): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-28 19:24:52.239: E/AndroidRuntime(29515): FATAL EXCEPTION: main
06-28 19:24:52.239: E/AndroidRuntime(29515): java.lang.NullPointerException
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.asthmaassistant.ShotActivity$StableArrayAdapter.getItemId(ShotActivity.java:104)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.AbsListView.obtainView(AbsListView.java:2180)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.ListView.onMeasure(ListView.java:1158)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Handler.handleCallback(Handler.java:725)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Looper.loop(Looper.java:137)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-28 19:24:52.239: E/AndroidRuntime(29515): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 19:24:52.239: E/AndroidRuntime(29515): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-28 19:24:52.239: E/AndroidRuntime(29515): at dalvik.system.NativeStart.main(Native Method)
06-28 19:24:55.409: I/Process(29515): Sending signal. PID: 29515 SIG: 9
06-28 19:24:57.329: E/Trace(29543): error opening trace file: No such file or directory (2)
06-28 19:24:58.129: D/dalvikvm(29543): GC_FOR_ALLOC freed 71K, 8% free 2498K/2692K, paused 77ms, total 82ms
06-28 19:24:58.161: I/dalvikvm-heap(29543): Grow heap (frag case) to 3.470MB for 960016-byte allocation
06-28 19:24:58.309: D/dalvikvm(29543): GC_FOR_ALLOC freed 2K, 6% free 3433K/3632K, paused 146ms, total 146ms
06-28 19:24:58.439: D/dalvikvm(29543): GC_CONCURRENT freed <1K, 6% free 3433K/3632K, paused 16ms+19ms, total 132ms
06-28 19:24:59.479: I/Choreographer(29543): Skipped 49 frames! The application may be doing too much work on its main thread.
06-28 19:24:59.609: D/gralloc_goldfish(29543): Emulator without GPU emulation detected.
logcat的: 06-30 19:25:34.192:E / AndroidRuntime(9478):java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。 [在ListView(2131230742,类android.widget.ListView)中使用Adapter(类com.asthmaassistant.StableArrayAdapter)]
答案 0 :(得分:0)
当您单击按钮时,“input”变量为null,您将数据存储在onCreate中的变量中,实际上您应该在onClick方法中执行此操作。
把你的
String input = editText.getText().toString();
onClick方法中的代码。
我认为问题在于存储ID的方式,您必须将新添加的项ID传递给适配器。只有在调用适配器的构造函数时才会获得ID。
您正在获取空指针,因为在添加新项目后,您的构造函数未被调用,因此无法找到新添加项目的ID。