gpt4 book ai didi

android - 构造函数 AlertDialog.Builder(LayoutOne, int) 未定义

转载 作者:行者123 更新时间:2023-11-29 16:13:45 25 4
gpt4 key购买 nike

我是编程新手,在让 AlertDialog 正常工作时遇到了一些困难。我显示了六个按钮,我想在单击第六个按钮时添加警报对话框。但是我收到一条错误消息“构造函数 AlertDialog.Builder(LayoutOne, int) 未定义”。谁能帮帮我?

谢谢!

public class LayoutOne extends Fragment implements OnClickListener {

public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();

return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
((Button) root.findViewById(R.id.drink)).setOnClickListener(this);
((Button) root.findViewById(R.id.sleep)).setOnClickListener(this);
((Button) root.findViewById(R.id.food)).setOnClickListener(this);
((Button) root.findViewById(R.id.events)).setOnClickListener(this);
((Button) root.findViewById(R.id.beach)).setOnClickListener(this);
((Button) root.findViewById(R.id.map)).setOnClickListener(this);

return root;
}

public void onClick(View v) {
// TODO Auto-generated method stub
Button tmp = (Button) v;
Button map = (Button) v;
if (tmp.getId() == R.id.drink) {
startActivity(new Intent("com.creatriks.zrce.DRINK"));
}
if (tmp.getId() == R.id.sleep) {
startActivity(new Intent("com.creatriks.zrce.SLEEP"));
}
if (tmp.getId() == R.id.food) {
startActivity(new Intent("com.creatriks.zrce.FOOD"));
}


if (tmp.getId() == R.id.events) {
startActivity(new Intent("com.creatriks.zrce.EVENTS"));
}
if (tmp.getId() == R.id.beach) {
startActivity(new Intent("com.creatriks.zrce.BEACH"));
}

if (map.getId() == R.id.map) {



AlertDialog alert = new AlertDialog.Builder(LayoutOne.this, AlertDialog.THEME_HOLO_LIGHT).create();
alert.setTitle("Options");
alert.setMessage("Choose an action");

startActivity(new Intent("com.creatriks.zrce.MAP"));



}
}

最佳答案

使用 getActivity() 返回与 fragment 关联的 Activity 。

改变这个

AlertDialog alert = new AlertDialog.Builder(LayoutOne.this, AlertDialog.THEME_HOLO_LIGHT).create();

为此

AlertDialog alert = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT).create();

关于android - 构造函数 AlertDialog.Builder(LayoutOne, int) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11069767/

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