android.view.InflateException二进制XML文件行#26膨胀类EditText时出错

时间:2018-11-24 02:01:50

标签: java android xml android-studio android-edittext

解决这个问题我很累。应用程序不执行它总是向我显示此错误二进制XML文件第26行:膨胀类EditText时出错。我不知道如何解决此问题。 这是我的activity_members.xml

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

    <android.support.constraint.ConstraintLayout
        android:id="@+id/newMeetScreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        tools:layout_editor_absoluteX="384dp">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="0dp"
            android:layout_height="54dp"
            android:src="@drawable/gradient"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/edit_search"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:layout_marginEnd="13dp"
            android:layout_marginBottom="7dp"
            android:background="@drawable/radius_edit_text"
            android:drawableLeft="@drawable/ic_search"
            android:drawablePadding="3dp"
            android:hint="Поиск"
            android:imeOptions="actionGo"
            android:inputType="text"
            android:textColor="@color/colorPrimaryDark"
            android:textColorHint="#9b9a9a"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btnDone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/btnDone"
            android:layout_width="52dp"
            android:layout_height="50dp"
            android:background="@android:drawable/list_selector_background"
            android:src="@drawable/ic_done"
            android:textColor="@color/colorProjectTextWhite"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/edit_search"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <android.support.v7.widget.RecyclerView
        android:background="@color/colorBackgroundLight"
        android:id="@+id/membersList"
        android:scrollbars="vertical"
        android:layout_marginTop="0dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

这是我的MembersActivity.java类。

public class MembersActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_READ_CONTACTS = 1;
    private static boolean READ_CONTACTS_GRANTED = false;
    RecyclerView members;
    ArrayList<String> contacts = new ArrayList<String>();
    ArrayList<String> tell = new ArrayList<String>();
    UserAdapter adapter;
    User user;
   ArrayList<User> users = new ArrayList<>();
    ImageButton btnDone;
    StringBuilder stringBuilder = null;
    EditText editSearch;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members);
        members = findViewById(R.id.membersList);
        btnDone = findViewById(R.id.btnDone);
        editSearch = findViewById(R.id.edit_search);
        int hasReadContactPermission = ContextCompat.checkSelfPermission(Objects.requireNonNull(this), Manifest.permission.READ_CONTACTS);
        if (hasReadContactPermission == PackageManager.PERMISSION_GRANTED) {
            READ_CONTACTS_GRANTED = true;

        } else {

            ActivityCompat.requestPermissions(Objects.requireNonNull(this), new
                    String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_READ_CONTACTS);

        } 
        if (READ_CONTACTS_GRANTED) {
            getContacts();
        }

        btnDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stringBuilder = new StringBuilder();
                int i=0;
                char ch = ',';
                do{
                    User user = adapter.checkedUsers.get(i);
                    if(i == adapter.checkedUsers.size()-1) ch = '.';
                    stringBuilder.append(user.getName() + ch);
                    if(i != adapter.checkedUsers.size() -1) {
                        stringBuilder.append("\n");
                    }
                    i++;

                } while (i < adapter.checkedUsers.size());
                if(adapter.checkedUsers.size() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra("names", stringBuilder.toString());
                    setResult(RESULT_OK, intent);
                    finish();


                } else {
                    Toast.makeText(MembersActivity.this, "Пожалуйста, выберите друзей", Toast.LENGTH_LONG).show();

                }
            }
        });
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                adapter.getFilter().filter(charSequence);
                return;

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_READ_CONTACTS:
                if (grantResults.length > 0 && grantResults[0] ==
                        PackageManager.PERMISSION_GRANTED) {
                    READ_CONTACTS_GRANTED = true;

                }
        }
        if (READ_CONTACTS_GRANTED) {
            getContacts();
        }
        else {
            Toast.makeText(this, "Требуется установить разрешения", Toast.LENGTH_LONG).show();
        }
    }

    public void getContacts() {
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Uri PhoneCONTENT_URI =
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID =
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        StringBuffer output = new StringBuffer();
        ContentResolver contentResolver = Objects.requireNonNull(this).getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

        if (cursor.getCount() > 0) {

            while (cursor.moveToNext()) {
                String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

                if (hasPhoneNumber > 0) {
                    contacts.add(name);

                    Cursor phoneCursor =
                            contentResolver.query(PhoneCONTENT_URI, null,
                                    Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
                    while (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                        tell.add(phoneNumber);
                    }
                }
            }
        }

        if(tell.size() > contacts.size()) {
            for(int i=0; i <contacts.size(); i++) {
                user = new User(tell.get(i), contacts.get(i));
                users.add(user);
            }
        } else {
            for(int i=0; i < tell.size(); i++) {
                user = new User(tell.get(i), contacts.get(i));
                users.add(user);
            }
        }

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        adapter = new UserAdapter(users, this);
        members.setLayoutManager(mLayoutManager);
        members.setItemAnimator(new DefaultItemAnimator());
        members.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        members.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


}

请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

您需要在布局文件中设置EditText的宽度和高度,但不能将这些属性设置为0dp。如果要根据屏幕尺寸进行设置,则可以在xml中使用layout_weight。Here you can know about layout_weight usage

如果在布局中使用constraintLayout,则可以看到此链接。 Responsive layout designing with constraint layout