gpt4 book ai didi

android - .notifyDataSetChanged()问题

转载 作者:行者123 更新时间:2023-12-03 06:19:14 27 4
gpt4 key购买 nike

我正在制作一个Android来做名为list track的列表应用程序,现在我仍在研究该应用程序的基础知识。无论如何,我为列表 View 创建了一个上下文菜单,该上下文菜单只有1个选项“删除”,它的意思是从列表中删除一个项目,但是当单击“删除”选项时,应用程序崩溃。我尝试在没有.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()。谢谢

最佳答案

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

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

关于android - .notifyDataSetChanged()问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com