gpt4 book ai didi

android - alertDialogue 中按钮的 OnclickListner 出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 15:26:14 26 4
gpt4 key购买 nike

我在 alertbuilder 中填充的布局有两个按钮。但我无法为此设置 onClickListner。发生此异常。请看我的代码。自定义 alertDialogue 的 XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_common"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<EditText
android:id="@+id/user_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:singleLine="true" >

<requestFocus />
</EditText>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />

<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>

AlertDialog.Builder alert = new AlertDialog.Builder(Myclass.this);
alert.setTitle("title");
alert.setIcon(iconImage);
LayoutInflater inflater = (LayoutInflater)MyClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_dialogue, null, false);
user_input= (EditText)view.findViewById(R.id.user_text);
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {

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

}
});

if(content.equals("")) {
user_input.setHint(hint);
} else {
user_input.setText(content);
}
alert.setView(view);
searchAlert = alert.create();
searchAlert.show();

最佳答案

您在 findViewById 之前缺少 view,通过排除 view 您指的是 Activity ..:

Button cancel = (Button) view.findViewById(R.id.cancel);

关于android - alertDialogue 中按钮的 OnclickListner 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706234/

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