Вопрос

Макет, который я надуваю в AlertBuelder, имеет две кнопки.Но я не могу настроить onclickListner для этого.Это исключение происходит.Пожалуйста, смотрите мой код. 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();
.

Это было полезно?

Решение

Вы отсутствуете view перед findViewById, исключая view, вы ссылаетесь на деятельность ..:

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

Другие советы

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

Проверьте здесь

user_input= (EditText)view.findViewById(R.id.user_text);//defined view for user_input
Button cancel = (Button) findViewById(R.id.cancel);//not defined view for button
.

Изменить на

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top