gpt4 book ai didi

java - 如何处理android中的致命异常(坏 token 异常)

转载 作者:行者123 更新时间:2023-11-30 01:49:22 27 4
gpt4 key购买 nike

这是我在单击按钮时删除所选项目的代码,我在此代码中添加了警报对话框,添加此警报框后,发生 fatal error 异常。

 public class MycustomAdapter extends BaseAdapter implements ListAdapter {
public ArrayList<category> list = new ArrayList<category>();
public Context context;



public MycustomAdapter(ArrayList<category> list, Context context) {
this.list = list;
this.context = context;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int pos) {
return list.get(pos);
}

@Override
public long getItemId(int position) {
return list.get(position).getId();
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row, null);
}

//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.lblreload
);
listItemText.setText(list.get(position).getName());

listItemText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
category row = (category) list.get(position);
int selected_id = row.getId();
String budget = row.getName();

Intent myIntent = new Intent(context, addbudget.class);
myIntent .putExtra("passed data key",budget);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
});

//Handle buttons and add onClickListeners
TextView deleteBtn = (TextView)view.findViewById(R.id.delete_btn);

deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SQLiteDatabase db = new DBhelper(context).getWritableDatabase();
db.delete(DBhelper.TABLE1, DBhelper.C_ID + "=?", new String[] {Integer.toString(list.get(position).getId())});
db.close();
list.remove(position);
notifyDataSetChanged();

}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

}
});


return view;
}
}

这是我点击删除按钮时得到的 fatal error 异常。

 22 20:50:56.546  28837-28837/com.example.username.weddingplanning
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:811)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
at android.app.Dialog.show(Dialog.java:282)
at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
at com.example.username.weddingplanning.MycustomAdapter$2.onClick(MycustomAdapter.java:101)
at android.view.View.performClick(View.java:4439)
at android.view.View$PerformClick.run(View.java:18398)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

我已经在 stack overflow 中解决了类似的问题,但我没有得到它。我在 CustomAdapter 类中有这个删除按钮,而不是在 Activity 类中。

最佳答案

AlertDialog.Builder 不仅需要任何上下文,它还需要一个 Activity 。更准确地说,它需要显示 ListView 的 Activity 。

所以问题是“我们如何从上下文中获取 Activity ”?

首先,您需要使用包含 ListView 的 Activity 来初始化您的适配器:

// assuming 'this' means the activity:
adapter = new MyCustomAdapter(myList, this);

然后您可以在“MyCustomAdapter.java”的“getView()”方法中执行以下操作(假设 Activity 名为“MyActivity”):

deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {

// you know the condition will be true,
// but for formal reasons make sure anyway:
if (context instanceof MyActivity)
{
new AlertDialog.Builder((MyActivity)context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// and so on...
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

}
}
});

为了安全起见,数据库调用还是可以这样写

SQLiteDatabase db = new DBhelper(context.getApplicationContext()).getWritableDatabase();

关于java - 如何处理android中的致命异常(坏 token 异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33285054/

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