ContextMenu 无法使用自定义适配器从 ListView 中删除行
ContextMenu Fails To Remove A Row From A ListView With A Custom Adapter
问题是:我的 ContextMenu 无法使用自定义适配器从 ListView 中删除行,应用程序因 NullPointerException 而崩溃,请参阅 Logcat 输出。
我进行了 Google 搜索并搜索了 Whosebug。 None 我找到的信息解决了这个问题。
我的问题是:我的代码有什么问题?
请提供正确的代码来解决这个问题。
Java代码:classTestActivity
public class TestActivity extends ListActivity {
private String itemNameArray[];
private String dateArray[];
private ListView listview;
CustomListViewAdapter customListViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerForContextMenu(getListView());
root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
getDir(root);
listview = getListView();
name=itemNameArray;
lastmod=dateArray;
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
listview.setAdapter(customListViewAdapter);
} // End of onCreate code.
class CustomListViewAdapter extends ArrayAdapter<String> {
Context context;
String[] nameArray;
String[] modifiedArray;
CustomListViewAdapter(Context c, String[] name, String[] lastmod)
{
super(c, R.layout.layout_item_view, R.id.rowtext, name);
this.context = c;
this.nameArray = name;
this.modifiedArray = lastmod;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.layout_item_view, parent, false);
TextView myName;
TextView myLastMod;
myName=(TextView) row.findViewById(R.id.rowtext);
myName.setText(nameArray[position]);
myLastMod=(TextView) row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray[position]);
return row;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem itemMenu) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)itemMenu.getMenuInfo();
switch (itemMenu.getItemId()) {
case R.id.context_menu_rename:
Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
return true;
case R.id.context_menu_delete:
// NOTE TESTING the line of code below caused a runtime error: java.lang.NullPointerException
customListViewAdapter.remove(customListViewAdapter.getItem(info.position));
customListViewAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "position = " + info.position, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
return true;
return super.onContextItemSelected(itemMenu);
}
}
}
Logcat 输出:
11-12 12:46:59.965 14265-14265/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
11-12 12:46:59.981 14265-14265/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.testing.listapp, PID: 14265
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.testing.listapp.TestActivity$CustomListViewAdapter.remove(java.lang.Object)' on a null object reference
at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5454)
at android.app.Activity.onMenuItemSelected(Activity.java:2905)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
at com.android.internal.app.AlertController$AlertParams.onItemClick(AlertController.java:1082)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Logcat 输出 #2 :
11-13 10:45:21.229 21432-21432/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
11-13 10:45:21.246 21432-21432/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.testing.listapp, PID: 21432
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
at java.util.AbstractCollection.remove(AbstractCollection.java:229)
at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244)
at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5468)
at android.app.Activity.onMenuItemSelected(Activity.java:2905)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
at com.android.internal.app.AlertController$AlertParams.onItemClick(AlertController.java:1082)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
尝试换行
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
至:
customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
好奇是不是局部变量,反正实例变量CustomListViewAdapter已经初始化了
看看著名的SOpost,通常在有NPE的时候给出
问题是:我的 ContextMenu 无法使用自定义适配器从 ListView 中删除行,应用程序因 NullPointerException 而崩溃,请参阅 Logcat 输出。
我进行了 Google 搜索并搜索了 Whosebug。 None 我找到的信息解决了这个问题。
我的问题是:我的代码有什么问题? 请提供正确的代码来解决这个问题。
Java代码:classTestActivity
public class TestActivity extends ListActivity {
private String itemNameArray[];
private String dateArray[];
private ListView listview;
CustomListViewAdapter customListViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerForContextMenu(getListView());
root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
getDir(root);
listview = getListView();
name=itemNameArray;
lastmod=dateArray;
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
listview.setAdapter(customListViewAdapter);
} // End of onCreate code.
class CustomListViewAdapter extends ArrayAdapter<String> {
Context context;
String[] nameArray;
String[] modifiedArray;
CustomListViewAdapter(Context c, String[] name, String[] lastmod)
{
super(c, R.layout.layout_item_view, R.id.rowtext, name);
this.context = c;
this.nameArray = name;
this.modifiedArray = lastmod;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.layout_item_view, parent, false);
TextView myName;
TextView myLastMod;
myName=(TextView) row.findViewById(R.id.rowtext);
myName.setText(nameArray[position]);
myLastMod=(TextView) row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray[position]);
return row;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem itemMenu) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)itemMenu.getMenuInfo();
switch (itemMenu.getItemId()) {
case R.id.context_menu_rename:
Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
return true;
case R.id.context_menu_delete:
// NOTE TESTING the line of code below caused a runtime error: java.lang.NullPointerException
customListViewAdapter.remove(customListViewAdapter.getItem(info.position));
customListViewAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "position = " + info.position, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
return true;
return super.onContextItemSelected(itemMenu);
}
}
}
Logcat 输出:
11-12 12:46:59.965 14265-14265/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
11-12 12:46:59.981 14265-14265/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.testing.listapp, PID: 14265
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.testing.listapp.TestActivity$CustomListViewAdapter.remove(java.lang.Object)' on a null object reference
at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5454)
at android.app.Activity.onMenuItemSelected(Activity.java:2905)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
at com.android.internal.app.AlertController$AlertParams.onItemClick(AlertController.java:1082)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Logcat 输出 #2 :
11-13 10:45:21.229 21432-21432/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
11-13 10:45:21.246 21432-21432/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.testing.listapp, PID: 21432
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
at java.util.AbstractCollection.remove(AbstractCollection.java:229)
at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244)
at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5468)
at android.app.Activity.onMenuItemSelected(Activity.java:2905)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
at com.android.internal.app.AlertController$AlertParams.onItemClick(AlertController.java:1082)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
尝试换行
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
至:
customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
好奇是不是局部变量,反正实例变量CustomListViewAdapter已经初始化了
看看著名的SOpost,通常在有NPE的时候给出