Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog view must contain an EditText with id @android:id/edit

I'm using support:preference-v7:25.0.1 and in my app I'm using an PreferenceActivity to set username with an EditText. When I run my app, at runtime I get the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
      Process: it.company.android.aaaa, PID: 8023
      java.lang.IllegalStateException: Dialog view must contain an EditText with id @android:id/edit
          at android.support.v7.preference.EditTextPreferenceDialogFragmentCompat.onBindDialogView(EditTextPreferenceDialogFragmentCompat.java:67)
          at android.support.v7.preference.PreferenceDialogFragmentCompat.onCreateDialog(PreferenceDialogFragmentCompat.java:148)
          at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:312)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
          at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:157)
          at android.app.ActivityThread.main(ActivityThread.java:5633)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
          at dalvik.system.NativeStart.main(Native Method)

Why I get this error? How can I resolve this problem? Thank you to all.

like image 554
Insoft Avatar asked Dec 13 '16 14:12

Insoft


1 Answers

Sadly, this is a mistake on Google and I have filed a bug issue https://code.google.com/p/android/issues/detail?id=231576

The hack to get around this in the mean time is to:

Create this layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:layout_marginBottom="48dp"
    android:overScrollMode="ifContentScrolls">

    <LinearLayout
        android:id="@+id/edittext_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:orientation="vertical">

        <EditText
            android:id="@android:id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="4dp" />

    </LinearLayout>

</ScrollView>

Then after you create your object (EditTextPreference editTextPreference = new EditTextPreference(context)), call setDialogLayoutResource(R.layout.THE_LAYOUT_ABOVE);

This will resolve the issue for now

like image 101
DrRiddles Avatar answered Nov 08 '22 19:11

DrRiddles