경고 다이얼로그에 있는 버튼의 온클릭리스트에 있는 널 포인터 인터셉션

StackOverflow https://stackoverflow.com//questions/12706234

문제

내가 경고 빌더로 팽창하고있는 레이아웃은 두 개의 버튼이 있습니다.그러나 나는 그것을 위해 클릭 리스트를 설정할 수 없습니다.이 예외가 발생합니다.내 코드를 참조하십시오.사용자 지정 경고 다이얼로그.

<?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