我才刚刚开始学习Android,因此,如果我不解释自己想做的很好的话,我深表歉意。因此,在我的预置列表活动中,我有一个预置对象的数组列表,并使用来自sqlite数据库的值填充该数组列表,该数据库是在相应的DBHelper中创建和定义的。我有一个使用自定义视图(textView和imageButton)的presetAdapter。我将imageButtons设置为删除图标,当单击该按钮时,从数据库中删除相应的textview,使用更新后的值重新填充arraylist,然后对其自身进行更新,以显示该值的删除。我能够从数据库中正确删除该值,并且为了重新填充和刷新arraylist和presetAdapter,我在presetList中创建了一个称为refreshList()的方法。此方法应该处理该功能,但是当我调用它时,它将引发NullPointerException。更具体地说,当我尝试打开游标以从数据库中读取值时,它崩溃了:错误:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dmapp, PID: 5697 java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:327) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352) at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:322) at com.example.dmapp.presetsDBHelper.getSpecificPresets(presetsDBHelper.java:93) at com.example.dmapp.PresetList.refreshList(PresetList.java:77) at com.example.dmapp.PresetAdapter$2.onClick(PresetAdapter.java:72)这是PresetList:public class PresetList extends AppCompatActivity { MyRecyclerViewAdapter adapter; private PresetAdapter presetAdapter; private ArrayList<Preset> presetList = new ArrayList<>(); String TAG = "PresetList"; private String passedPresetVariable; private presetsDBHelper mPresetsDBHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preset_list); setPassedPresetVariable(this.getIntent().getStringExtra("presetVariable")); Log.d(TAG, "onCreate: Passed Preset variable is " + getPassedPresetVariable()); // data to populate the RecyclerView with mPresetsDBHelper = new presetsDBHelper(this); DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics(); int deviceHeight = (displayMetrics.heightPixels); Button addEntry = findViewById(R.id.addEntry); ListView presetListView = findViewById(R.id.presetListView); ViewGroup.LayoutParams params = presetListView.getLayoutParams(); params.height = (int)(deviceHeight * .80); presetListView.setLayoutParams(params); // set up the RecyclerView with data from the database //Log.d(TAG, "onCreate: Preset variable is " + getIntent().getStringExtra("presetVariable")); Cursor presets = mPresetsDBHelper.getSpecificPresets(getIntent().getStringExtra("presetVariable")); try { if (presets.moveToNext()) { do { presetList.add(new Preset(presets.getString(2))); } while (presets.moveToNext()); } } finally { presets.close(); } presetAdapter = new PresetAdapter(this, presetList); presetListView.setAdapter(presetAdapter); /* addEntry.setOnClickListener(new View.OnClickListener() { // The code in this method will be executed when the numbers View is clicked on. @Override public void onClick(View view) { Intent intent = new Intent(view.getContext(), CityInfo.class); startActivity(intent); } }); */ } public void refreshList(){ mPresetsDBHelper = new presetsDBHelper(this); Log.d(TAG, "refreshList: Preset variable is " + getPassedPresetVariable()); presetList.clear(); Cursor presets = mPresetsDBHelper.getSpecificPresets("Environment"); //Didn't work with getPassedPresetVariable, attempted hardcoded value to test. Still not functional try { if (presets.moveToNext()) { do { presetList.add(new Preset(presets.getString(2))); } while (presets.moveToNext()); } } finally { presets.close(); } presetAdapter.notifyDataSetChanged(); } public void setPassedPresetVariable(String passedPresetVariable) { this.passedPresetVariable = passedPresetVariable; } public String getPassedPresetVariable() { return passedPresetVariable; }}这是我的适配器:public class PresetAdapter extends ArrayAdapter<Preset> { private Context mContext; private List<Preset> presetList = new ArrayList<>(); private PresetList presetListClass; String TAG = "PresetAdapter"; public PresetAdapter(Context context, ArrayList<Preset> list) { super(context, 0 , list); mContext = context; presetList = list; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { View listItem = convertView; if(listItem == null) { listItem = LayoutInflater.from(mContext).inflate(R.layout.preset_item, parent, false); } Preset currentPreset = presetList.get(position); final TextView name = listItem.findViewById(R.id.presetValue); ImageButton delete = listItem.findViewById(R.id.deleteIcon); name.setText(currentPreset.getValue()); name.setTag(position); name.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { int position = (Integer) view.getTag(); // Access the row position here to get the correct data item Preset preset = getItem(position); // Do what you want here... String presetValue = preset.getValue(); Intent intent = new Intent(name.getContext(), CityInfo.class); intent.putExtra("presetValue", presetValue); mContext.startActivity(intent); } }); delete.setTag(position); delete.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { int position = (Integer) view.getTag(); // Access the row position here to get the correct data item Preset preset = getItem(position); // Do what you want here... String presetValue = preset.getValue(); presetsDBHelper mPresetsDBHelper = new presetsDBHelper(name.getContext()); mPresetsDBHelper.removeSpecificPreset(presetValue); presetListClass = new PresetList(); presetListClass.refreshList(); } }); return listItem; }}我认为有一个更简单的方法可以解决此问题,或者有一些更简单的解决方案。如果有人可以给我一些见识,将不胜感激! 最佳答案 抱歉,如果我的问题看起来很愚蠢,但这是我上课时的一个常见错误,所以我问:您知道光标从0开始吗?那么2实际上是第三个字段?游标的工作方式类似于数组索引。也许对你来说这很明显,我向你道歉。但是我的同班同学中有一半正因此而面临挑战。而且我不确定extended ArrayAdapter 是否应指定类型?我也是新手,所以我也不知道,但是当我使用适配器时,我没有指定并且它正在工作关于java - 如何在由自定义arrayadapter填充的 ListView 中的每个按钮上添加onclick,以从数组中删除该项目并进行更新?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58702194/
10-10 19:23