imageView не отображается в сцене предварительного просмотра, но все еще отображается в эмуляторе - PullRequest
0 голосов
/ 29 июня 2018

Я пытаюсь создать игру, похожую на крестики-нолики, используя GridLayout. Экран предварительного просмотра может отображать фоновое изображение для GridLayout (доска), но не может отображать imageView, который я вставил в GridLayout (красный O). В окне сообщения также есть сообщение об ошибке:

java.lang.ClassNotFoundException: android.view.View $ OnUnhandledKeyEventListener

Я попытался обновить макет и перестроить проект, но все еще не могу исправить ошибку.

Экран предварительного просмотра сравнивается с экраном эмулятора, где отображаются два изображения

enter image description here

Код для activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <android.support.v7.widget.GridLayout
        android:layout_width="368dp"
        android:layout_height="368dp"
        android:background="@drawable/board"
        app:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="3">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />
    </android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>

Ответы [ 5 ]

0 голосов
/ 17 июля 2018

Для тех, кто наткнулся на эту ошибку. Измените android.support.v7.widget.GridLayout на GridLayout только в XML. У меня была та же проблема, и это было решением.

0 голосов
/ 29 июня 2018

Попробуйте этот код и измените только GridLayout take .. Вы можете изменить корневую компоновку, если для constraintLayout используется любая компоновка.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">

<GridLayout
    android:layout_width="368dp"
    android:layout_height="368dp"
    android:columnCount="3"
    android:rowCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

</GridLayout>

0 голосов
/ 29 июня 2018

Просто поменяйте <android.support.v7.widget.GridLayout

<GridLayout
        android:layout_width="368dp"
        android:layout_height="368dp"
        android:background="@drawable/board"
        android:columnCount="3"
        android:rowCount="3"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />
    </GridLayout>

Этот показ предварительного просмотра для меня. Попробуй это. Также проверьте версию библиотеки зависимостей
implementation 'com.android.support.constraint:constraint-layout:1.1.0'

0 голосов
/ 29 июня 2018

В моей Android Studio ваш xml отображается как в режиме предварительного просмотра, так и в эмуляторе. Попробуйте сделать следующее.

1) Добавьте к вам build.gradle (Module.app) снова эти зависимости

implementation 'com.android.support:gridlayout-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'

2) После успешного импорта зависимостей обновите Android Studio

Надеюсь, что это работает!

0 голосов
/ 29 июня 2018

установить ориентацию для расположения сетки

<GridLayout
    android:columnCount="3"
    android:rowCount="3"
    android:orientation="horizontal"
     />
...