我有一个Activity类,我正在调用ListView
适配器。在适配器类中,我在每行中有两个按钮的onClick
方法。当我单击一个按钮时,我从数据库中删除了该行,但我必须在Activity类中调用该方法,以便更新UI,但是调用此方法是这样的:
MainActivity main = new MainActivity();
main.removeTimer();
引发NullPointerException
。这是我在Activity Class中的方法:
public void removeTimer() {
Cursor result1 = myDb.rawQuery("SELECT * from Timers", null);
result1.moveToFirst();
timers.clear();
names.clear();
while (result1.isAfterLast() == false) {
timers.add(result1.getString(result1.getColumnIndex(TIME_COLUMN)));
names.add(result1.getString(result1.getColumnIndex(NAME_COLUMN)));
result1.moveToNext();
}
timerRow.clear();
for (int i = 0; i < timers.size(); i++) {
TimerRow item = new TimerRow(timers.get(i), names.get(i), bRemove,
bStartStop);
timerRow.add(item);
}
adapter.notifyDataSetChanged();
}
如果我在Activity类中调用它,它的效果非常好。这就是我从ListViewAdapter
类调用该方法的方法:
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final TimerRow rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.timer_row, null);
holder = new ViewHolder();
// make layout params
holder.txtTimer = (TextView) convertView.findViewById(R.id.tvTimes);
holder.txtTimer.getLayoutParams().height = height / 10;
holder.txtName = (TextView) convertView.findViewById(R.id.tvName);
holder.txtName.getLayoutParams().height = height / 10;
holder.bRemove = (Button) convertView.findViewById(R.id.bRemoveTimer);
holder.bRemove.getLayoutParams().height = height / 10;
holder.bStartStop = (Button) convertView
.findViewById(R.id.bStartStopTimer);
holder.bStartStop.getLayoutParams().height = height / 10;
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtName.setText(rowItem.getName());
holder.txtTimer.setText(rowItem.getTimer());
holder.bRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String name = holder.txtName.getText().toString();
final String timer = holder.txtTimer.getText().toString();
final SQLiteDatabase myDb = context.openOrCreateDatabase("timersDB", NO_SELECTION, null);
//final int pos = position;
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_timer);
builder.setMessage(context.getString(R.string.want_to_delete) + " " + name
+ " " + context.getString(R.string.timer2));
builder.setPositiveButton(R.string.delete,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
myDb.execSQL("DELETE FROM Timers WHERE "
+ TIME_COLUMN + " = '" + timer
+ "' AND " + NAME_COLUMN + " = '"
+ name + "';");
// THIS IS WHERE THE METHOD IS CALLED
MainActivity main = new MainActivity();
main.removeTimer();
}
});
alert = builder.create();
alert.show();
}
这是我的日志:
02-22 17:46:15.300: E/AndroidRuntime(4508): FATAL EXCEPTION: main
02-22 17:46:15.300: E/AndroidRuntime(4508): Process: com.adrissa.kitchentimer, PID: 4508
02-22 17:46:15.300: E/AndroidRuntime(4508): java.lang.NullPointerException
02-22 17:46:15.300: E/AndroidRuntime(4508): at com.albert.timer.MainActivity.removeTimer(MainActivity.java:115)
02-22 17:46:15.300: E/AndroidRuntime(4508): at com.albert.timer.CustomTimerRowAdapter$2$2.onClick(CustomTimerRowAdapter.java:272)
02-22 17:46:15.300: E/AndroidRuntime(4508): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
02-22 17:46:15.300: E/AndroidRuntime(4508): at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 17:46:15.300: E/AndroidRuntime(4508): at android.os.Looper.loop(Looper.java:136)
02-22 17:46:15.300: E/AndroidRuntime(4508): at android.app.ActivityThread.main(ActivityThread.java:5586)
02-22 17:46:15.300: E/AndroidRuntime(4508): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 17:46:15.300: E/AndroidRuntime(4508): at java.lang.reflect.Method.invoke(Method.java:515)
02-22 17:46:15.300: E/AndroidRuntime(4508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
02-22 17:46:15.300: E/AndroidRuntime(4508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
02-22 17:46:15.300: E/AndroidRuntime(4508): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
也许是因为您拨打main.removeTimer()
而不是mainAct.removeTimer()
?
答案 1 :(得分:0)
实际上这很简单,我花了5个小时才在Google上找到这个问题。
How to let OnClick in ListView's Adapter call Activity's function