gpt4 book ai didi

java - Alert Builder 我错过了什么

转载 作者:行者123 更新时间:2023-11-30 04:01:55 25 4
gpt4 key购买 nike

我正在尝试在单击的按钮下的 BaseAdapter 中添加 Alert Builder。这是我的代码

 Button btn=(Button)vi.findViewById(R.id.btnAdd);
btn.setOnClickListener(new OnClickListener(){

AlertDialog.Builder dialogBox=new AlertDialog.Builder(this);
dialogBox.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
String facebookFriendName = friendsName.getText().toString();
String FACEBOOKID = FBID;
Log.i("AKO SI: ", ""+facebookFriendName + FACEBOOKID);
}
});
dialogBox.setNegativeButton("No", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {

}
});
dialogBox.show();

现在我在这部分遇到错误 alert_box.show();

问题是什么?

编辑根据您的建议和意见,我更改了上面的代码并在我的 Activity 上添加 getter 和 setter

private Context context;  
/////////
public Context getContext() {
return context;
}

public void setContext(Context context) {
this.context = context;
}

在我的activity类下绑定(bind)

    adapter2 =new LazyAdapterGetFriends(this, songsList);   
adapter2.getContext();
list.setAdapter(adapter2);

但我仍然收到错误提示:

*09-11 13:48:49.298: E/AndroidRuntime(1226):    at com.fb.connect.LazyAdapterGetFriends$1.onClick(LazyAdapterGetFriends.java:95)

*pointing at AlertDialog.Builder dialogBox=new AlertDialog.Builder(context);

最佳答案

试试这个

  Button btn=(Button)vi.findViewById(R.id.btnAdd);
btn.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub


AlertDialog.Builder ab=new AlertDialog.Builder(MainActivity.this);
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
String facebookFriendName = friendsName.getText().toString();
String FACEBOOKID = FBID;
Log.i("AKO SI: ", ""+facebookFriendName + FACEBOOKID);
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
ab.show();
}
});

希望得到帮助

编辑

尝试 this.

AlertDialogs 只能使用 Activity 上下文创建,因此 getApplicationContext() 将不起作用。相反,将以下全局变量添加到您的 Activity 文件中:

Context mContext;

然后将以下内容添加到您的 onCreate() 中:

mContext = this;

现在,改变:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

关于java - Alert Builder 我错过了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363157/

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