ActionBar项目单击以将项目添加到ListView

时间:2014-08-26 17:23:15

标签: android android-listview android-actionbar android-adapterview

我在通过ActionBar项目点击添加新项目时遇到问题。我得到了下面的所有错误,代码就在下面。如果有人愿意告诉我我犯了什么愚蠢的错误,我将不胜感激。

logcat的:

08-26 19:17:57.767: E/AndroidRuntime(29108): FATAL EXCEPTION: main
08-26 19:17:57.767: E/AndroidRuntime(29108): Process: com.klangstudios.dailyachiever, PID: 29108
08-26 19:17:57.767: E/AndroidRuntime(29108): java.lang.NullPointerException
08-26 19:17:57.767: E/AndroidRuntime(29108):    at com.klangstudios.dailyachiever.Lista$StableArrayAdapter.getItemId(Lista.java:81)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.AbsListView.obtainView(AbsListView.java:2291)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.ListView.onMeasure(ListView.java:1175)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.View.measure(View.java:16540)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.View.measure(View.java:16540)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.View.measure(View.java:16540)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.View.measure(View.java:16540)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.View.measure(View.java:16540)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.Choreographer.doFrame(Choreographer.java:544)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.os.Handler.handleCallback(Handler.java:733)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.os.Looper.loop(Looper.java:136)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at android.app.ActivityThread.main(ActivityThread.java:5086)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at java.lang.reflect.Method.invoke(Method.java:515)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-26 19:17:57.767: E/AndroidRuntime(29108):    at dalvik.system.NativeStart.main(Native Method)

不知道为什么它不会工作:

public class Lista extends Activity{

ArrayList<String> list;
ListView listview;
StableArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista);     

    listview = (ListView) findViewById(R.id.listView1);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
        "Android", "iPhone", "WindowsMobile" };

    list = new ArrayList<String>();    
    adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                list.remove(item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
              }
            });
      }

    });

  }

  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;
    }

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle presses on the action bar items
      switch (item.getItemId()) {
          case R.id.action_add:
              doThis(); 
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }

  public void doThis(){
      list.add("Abba");
      adapter.notifyDataSetChanged();
    }     

}

2 个答案:

答案 0 :(得分:1)

返回mIdMap.get(item);

将这些行更改为

返回位置;

答案 1 :(得分:0)

问题出在您的适配器中。您在构造函数中创建了一个索引(mIdMap),但在调用adapter.notifyDataSetChanged()之前,您没有更新它。您的代码在getItemId()调用中抛出异常。

一个可行的解决方案是将此方法添加到您的适配器:

public void addToIdMap(String item) {
    // Add the new item to the map
    mIdMap.put(item, mIdMap.size());
    notifyDateSetChanged();
}

然后你可以这样称呼它:

list.add("Abba");
adapter.addToIdMap("Abba");

在这种情况下,我更喜欢使用BaseAdapter