문제

I have a dialog where I wish to force the user to fill in a name into an EditText, if it's empty I wish for pressing done to have no effect. However,

EditText x = (EditText) view.findViewById(R.id.name);

always returns null. I have tried searching but have only found the solution to add a view to findViewById(...) but I already have that. My guess it that it has to do with me creating my own version of the button.

This is the code for the Dialog.

package com.example.dialogruta;


@SuppressLint({ "NewApi", "ValidFragment" })
public class RecordDialog extends DialogFragment {


public  Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.record_fragment, null);
    builder.setView(view).setPositiveButton("Save experiment", null); //Creating my own 

    final Dialog d = builder.create();
    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = ((AlertDialog) d).getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    EditText x = (EditText) view.findViewById(R.id.name);
                    if( x!= null && !x.toString().isEmpty()){
                        String xs = x.getText().toString();
                        d.dismiss();
                    }
                    else{
                        //TODO: Stuff
                    }
                }
            });
        }
    });

    return d;
}


}

And this is the .xml-file

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


        <EditText
    android:id="@+id/name"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="Experiment name" />

        <EditText
    android:id="@+id/notes"
    android:inputType="textMultiLine"
    android:layout_width="match_parent"
    android:layout_height="200dip"
    android:layout_weight ="1"
    android:lines="5"
    android:minLines="1"
    android:gravity="top|left"
    android:maxLines="10"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="Notes" />

 </LinearLayout>
도움이 되었습니까?

해결책

Try this..

Change View name

final View main_view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(main_view).setPositiveButton("Save experiment", null);

and use it

EditText x = (EditText) main_view.findViewById(R.id.name);

because in your public void onClick(View view) { onclick has view that edit text need to refer inflated view. Not onclick view.

Or

As like @Raghunandan said change onclick view name as public void onClick(View v) {

다른 팁

Change this

public void onClick(View view) {

to

public void onClick(View v) {

You have

final View view = inflater.inflate(R.layout.record_fragment, null);

and this

public void onClick(View view) {

Both View view.

What happens here

 @Override
 public void onClick(View view) {
 EditText x = (EditText) view.findViewById(R.id.name);

EditText is not a child of button and view.findViewById looks for a view in the current view hierarchy. It should look for the view in the inflated layout final View view = inflater.inflate(R.layout.record_fragment, null);

change local view name in onClick() method.

public void onClick(View v)

Declare the variable as private at the onCreate method beginning and access there.

EditText x = (EditText) findViewById(R.id.name);

Now you will get the value of x in onClick() method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top