.notifyDataSetChanged()问题

时间:2014-12-14 19:02:28

标签: android gradle contextmenu notifydatasetchanged

我正在制作一个名为list track的安卓列表应用程序,现在我还在研究应用程序的基础知识。无论如何,我为列表视图创建了一个上下文菜单,上下文菜单只有1个选项“remove”,它意味着从列表中删除一个项目,但是,当点击“删除”选项时,应用程序崩溃。我尝试在没有.notifyDataSetChanged()的情况下运行它并且它工作但我必须在删除项目之前单击“添加”按钮,这会导致很多问题。 这是代码:

        package com.drift.listtrack;

      import android.app.*;
      import android.os.*;
      import android.widget.*;
      import java.util.*;
      import android.view.View.*;
      import android.view.*;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.ContextMenu;
      import android.widget.AdapterView;
      import android.view.ContextMenu.*;

 public class MainActivity extends Activity 
{
private ArrayList<String> todoItems;
private ListView list;
private EditText items;
private ArrayAdapter<String> aa;

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

    list= (ListView) findViewById(R.id.list);
    items= (EditText) findViewById(R.id.items);
    Button enter=(Button) findViewById(R.id.enter);

    todoItems= new ArrayList<String>();
    int resID=R.layout.listrack_item;
    final ArrayAdapter<String> aa=new ArrayAdapter<String>(this, resID, todoItems);
    list.setAdapter(aa);



    enter.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            todoItems.add(0,items.getText().toString());
            aa.notifyDataSetChanged();
            items.setText("");

        }
    });
    registerForContextMenu(list);

}

static final private int REMOVE_TODO= Menu.FIRST;



@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);

    menu.setHeaderTitle("Options");
    menu.add(0, REMOVE_TODO, Menu.NONE, R.string.remove); 

}


@Override
public boolean onContextItemSelected(MenuItem item)
{
    // TODO: Implement this method
     super.onContextItemSelected(item);
     switch (item.getItemId()){
         case (REMOVE_TODO):{
             AdapterView.AdapterContextMenuInfo menuInfo;
                 menuInfo=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

                 int index=menuInfo.position;

                 removeItem(index);
                 return true;
         }


     }


     return false;
}


private void removeItem(int _index){

    todoItems.remove(_index);
    aa.notifyDataSetChanged();

}



    }

这是我的logcat

    12-14 19:16:30.155 4786 4786 D     dalvikvm                                     Late-enabling CheckJNI
    12-14 19:16:30.575 4786 4786 D     dalvikvm                                     GC_FOR_ALLOC freed 74K, 10% free 7092K/7875K, paused 19ms, total 19ms
    12-14 19:16:30.580 4786 4786 I     dalvikvm-heap                                Grow heap (frag case) to 8.550MB for 1048592-byte allocation
    12-14 19:16:30.600 4786 4816 D     dalvikvm                                     GC_FOR_ALLOC freed <1K, 10% free 8116K/8967K, paused 21ms, total 21ms
    12-14 19:16:30.615 4786 4788 D     dalvikvm                                     GC_CONCURRENT freed 3K, 10% free 8116K/8967K, paused 2ms+1ms, total 13ms
    12-14 19:16:30.665 4786 4786 D     AbsListView                                  Get MotionRecognitionManager
    12-14 19:16:30.880 4786 4786 D     libEGL                                       loaded /system/lib/egl/libEGL_mali.so
    12-14 19:16:30.885 4786 4786 D     libEGL                                       loaded /system/lib/egl/libGLESv1_CM_mali.so
    12-14 19:16:30.890 4786 4786 D     libEGL                                       loaded /system/lib/egl/libGLESv2_mali.so
    12-14 19:16:30.895 4786 4786 D                                                  Device driver API match
    12-14 19:16:30.895 4786 4786 D                                                  Device driver API version: 10
    12-14 19:16:30.895 4786 4786 D                                                  User space API version: 10
    12-14 19:16:30.895 4786 4786 D                                                  mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
    12-14 19:16:30.965 4786 4786 D     OpenGLRenderer                               Enabling debug mode 0
    12-14 19:16:30.970 4786 4786 E     SensorManager                                thread start
    12-14 19:16:30.970 4786 4786 D     SensorManager                                registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41798610
    12-14 19:16:32.920 4786 4786 D     SensorManager                                unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41798610
    12-14 19:16:32.920 4786 4786 D     Sensors                                      Remain listener = Sending .. normal delay 200ms
    12-14 19:16:32.920 4786 4786 I     Sensors                                      sendDelay --- 200000000
    12-14 19:16:32.925 4786 4786 D     SensorManager                                JNI - sendDelay
    12-14 19:16:32.930 4786 4786 I     SensorManager                                Set normal delay = true
    12-14 19:16:34.520 4786 4786 W     IInputConnectionWrapper                      showStatusIcon on inactive InputConnection
    12-14 19:16:30.155 4786 4786 D     dalvikvm                                     Late-enabling CheckJNI
    12-14 19:16:30.575 4786 4786 D     dalvikvm                                     GC_FOR_ALLOC freed 74K, 10% free 7092K/7875K, paused 19ms, total 19ms
    12-14 19:16:30.580 4786 4786 I     dalvikvm-heap                                Grow heap (frag case) to 8.550MB for 1048592-byte allocation
    12-14 19:16:30.600 4786 4816 D     dalvikvm                                     GC_FOR_ALLOC freed <1K, 10% free 8116K/8967K, paused 21ms, total 21ms
    12-14 19:16:30.615 4786 4788 D     dalvikvm                                     GC_CONCURRENT freed 3K, 10% free 8116K/8967K, paused 2ms+1ms, total 13ms
    12-14 19:16:30.665 4786 4786 D     AbsListView                                  Get MotionRecognitionManager
    12-14 19:16:30.880 4786 4786 D     libEGL                                       loaded /system/lib/egl/libEGL_mali.so
    12-14 19:16:30.885 4786 4786 D     libEGL                                       loaded /system/lib/egl/libGLESv1_CM_mali.so
    12-14 19:16:30.890 4786 4786 D     libEGL                                       loaded /system/lib/egl/libGLESv2_mali.so
    12-14 19:16:30.895 4786 4786 D                                                  Device driver API match
    12-14 19:16:30.895 4786 4786 D                                                  Device driver API version: 10
    12-14 19:16:30.895 4786 4786 D                                                  User space API version: 10
    12-14 19:16:30.895 4786 4786 D                                                  mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
    12-14 19:16:30.965 4786 4786 D     OpenGLRenderer                               Enabling debug mode 0
    12-14 19:16:30.970 4786 4786 E     SensorManager                                thread start
    12-14 19:16:30.970 4786 4786 D     SensorManager                                registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41798610
    12-14 19:16:32.920 4786 4786 D     SensorManager                                unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41798610
    12-14 19:16:32.920 4786 4786 D     Sensors                                      Remain listener = Sending .. normal delay 200ms
    12-14 19:16:32.920 4786 4786 I     Sensors                                      sendDelay --- 200000000
    12-14 19:16:32.925 4786 4786 D     SensorManager                                JNI - sendDelay
    12-14 19:16:32.930 4786 4786 I     SensorManager                                Set normal delay = true
    12-14 19:16:34.520 4786 4786 W     IInputConnectionWrapper                      showStatusIcon on inactive InputConnection
    12-14 19:17:19.575 8036 8036 D     dalvikvm                                     DexOpt: load 22ms, verify+opt 34ms, 316316 bytes
    12-14 19:17:53.365 9751 9751 D     dalvikvm com.drift.listtrack                 Late-enabling CheckJNI
    12-14 19:17:53.645 9751 9751 D     dalvikvm com.drift.listtrack                 GC_FOR_ALLOC freed 78K, 10% free 7092K/7875K, paused 23ms, total 24ms
    12-14 19:17:53.645 9751 9751 I     dalvikvm-heap com.drift.listtrack            Grow heap (frag case) to 8.550MB for 1048592-byte allocation
    12-14 19:17:53.670 9751 9753 D     dalvikvm com.drift.listtrack                 GC_CONCURRENT freed 2K, 10% free 8114K/8967K, paused 2ms+2ms, total 20ms
    12-14 19:17:53.670 9751 9751 D     dalvikvm com.drift.listtrack                 WAIT_FOR_CONCURRENT_GC blocked 11ms
    12-14 19:17:53.670 9751 9770 D     dalvikvm com.drift.listtrack                 WAIT_FOR_CONCURRENT_GC blocked 8ms
    12-14 19:17:53.755 9751 9751 D     AbsListView com.drift.listtrack              Get MotionRecognitionManager
    12-14 19:17:53.820 9751 9751 D     libEGL com.drift.listtrack                   loaded /system/lib/egl/libEGL_mali.so
    12-14 19:17:53.825 9751 9751 D     libEGL com.drift.listtrack                   loaded /system/lib/egl/libGLESv1_CM_mali.so
    12-14 19:17:53.825 9751 9751 D     libEGL com.drift.listtrack                   loaded /system/lib/egl/libGLESv2_mali.so
    12-14 19:17:53.830 9751 9751 D      com.drift.listtrack                         Device driver API match
    12-14 19:17:53.830 9751 9751 D      com.drift.listtrack                         Device driver API version: 10
    12-14 19:17:53.830 9751 9751 D      com.drift.listtrack                         User space API version: 10
    12-14 19:17:53.830 9751 9751 D      com.drift.listtrack                         mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
    12-14 19:17:53.875 9751 9751 D     OpenGLRenderer com.drift.listtrack           Enabling debug mode 0
    12-14 19:17:53.880 9751 9751 E     SensorManager com.drift.listtrack            thread start
    12-14 19:17:53.880 9751 9751 D     SensorManager com.drift.listtrack            registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@417b9bb8
    12-14 19:18:00.040 9751 9751 D     AbsListView com.drift.listtrack              Get MotionRecognitionManager
    12-14 19:18:00.105 9751 9753 D     dalvikvm com.drift.listtrack                 GC_CONCURRENT freed 110K, 9% free 8432K/9223K, paused 3ms+3ms, total 24ms
    12-14 19:18:00.225 9751 9751 D     SensorManager com.drift.listtrack            unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@417b9bb8
    12-14 19:18:00.225 9751 9751 D     Sensors com.drift.listtrack                  Remain listener = Sending .. normal delay 200ms
    12-14 19:18:00.225 9751 9751 I     Sensors com.drift.listtrack                  sendDelay --- 200000000
    12-14 19:18:00.225 9751 9751 D     SensorManager com.drift.listtrack            JNI - sendDelay
    12-14 19:18:00.225 9751 9751 I     SensorManager com.drift.listtrack            Set normal delay = true
    12-14 19:18:00.900 9751 9751 D     AndroidRuntime com.drift.listtrack           Shutting down VM
    12-14 19:18:00.900 9751 9751 W     dalvikvm com.drift.listtrack                 threadid=1: thread exiting with uncaught exception (group=0x410352a0)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           FATAL EXCEPTION: main
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           java.lang.NullPointerException
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.drift.listtrack.MainActivity.removeItem(MainActivity.java:95)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.drift.listtrack.MainActivity.onContextItemSelected(MainActivity.java:80)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.app.Activity.onMenuItemSelected(Activity.java:2629)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4034)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:193)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:934)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.widget.AdapterView.performItemClick(AdapterView.java:301)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.widget.AbsListView$PerformClick.run(AbsListView.java:3078)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.widget.AbsListView$1.run(AbsListView.java:4161)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.os.Handler.handleCallback(Handler.java:615)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.os.Handler.dispatchMessage(Handler.java:92)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.os.Looper.loop(Looper.java:137)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at android.app.ActivityThread.main(ActivityThread.java:4921)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at java.lang.reflect.Method.invokeNative(Native Method)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at java.lang.reflect.Method.invoke(Method.java:511)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    12-14 19:18:00.905 9751 9751 E     AndroidRuntime com.drift.listtrack           at dalvik.system.NativeStart.main(Native Method)

有人能告诉我如何解决这个问题,我想我可能误用了notifyDataSetChanged()。感谢

1 个答案:

答案 0 :(得分:0)

你得到一个NullPointerException,因为一个对象(在类中)为null。原因是在onCreate中你创建了另一个具有相同名称的对象(arraylist)。您需要将arraylist分配给类变量。

尝试:           aa = new ArrayAdapter(this,resID,todoItems);