Android - Gridlayout android.support.v7.widget.GridLayout无法强制转换为android.widget.GridLayout

问题描述 投票:0回答:6

我想创建一个能够在所有API中运行的GridLayout。

问题是,当我使用GridLayout而不是android.support.v7.widget.GridLayout时,该应用程序在Android 7.1.1中正常运行,但在旧版本中它崩溃了。

但是,如果我使用android.support.v7.widget.GridLayout而不是GridLayout(如下面的代码),它总是崩溃。

activity_main

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#FFF"
    tools:context="devector.dom.gridtest.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">



    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textGrid"
            android:text="Grid test"
            android:textColor="#000"
            android:layout_centerInParent="true"
            android:textSize="34sp"
            />
    </RelativeLayout>

    <android.support.v7.widget.GridLayout
        android:textAlignment="center"
        android:id="@+id/mainGrid"
        android:padding="25dp"
        android:layout_marginLeft="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="2"
        app:rowCount="3"
        app:alignmentMode="alignMargins"
        app:columnOrderPreserved="false">


        <!-- Row 1 -->
        <!-- Column 1-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Zero"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 2 -->
        <!-- Column 2-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Two"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 3-->
        <!-- Column 3-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Is"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 4 -->
        <!-- Column 4-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="The"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 5 -->
        <!-- Column 5-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Besto"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 6 -->
        <!-- Column 6-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Waifu"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 7 -->
        <!-- Column 7-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Never"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 8 -->
        <!-- Column 8-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="Forget"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 9 -->
        <!-- Column 9-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="About"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <!-- Row 10 -->
        <!-- Column 10-->

        <android.support.v7.widget.CardView
            android:layout_width="150dp"
            android:layout_height="125dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp"
            >


            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:src="@drawable/zerotwo"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    />

                <TextView
                    android:text="It"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </android.support.v7.widget.CardView>
    </android.support.v7.widget.GridLayout>
    </LinearLayout>

</ScrollView>

日志

FATAL EXCEPTION: main
Process: devector.dom.gridtest, PID: 15773
java.lang.RuntimeException: Unable to start activity ComponentInfo{devector.dom.gridtest/devector.dom.gridtest.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6165)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
    at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)
    at android.app.Activity.performCreate(Activity.java:6687)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6165) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "devector.dom.gridtest"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:gridlayout-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'

}

main activity.Java

package devector.dom.gridtest;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    GridLayout mainGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainGrid = (GridLayout) findViewById(R.id.mainGrid);


        setSingleEvent(mainGrid);
    }


    private void setSingleEvent(GridLayout mainGrid) {

        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            CardView cardView = (CardView) mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                  if(finalI == 0) 
                  {
                      Intent intent = new Intent(MainActivity.this,Activity_one.class );
                      startActivity(intent);
                  } 

                }
            });
        }
    }
}
android android-gridlayout
6个回答
12
投票

从您的错误日志中是,

android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
                      at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)

你可以像这样改变你的演员来解决你的问题

android.support.v7.widget.GridLayout grid = (android.support.v7.widget.GridLayout)findViewById(R.id.grid);

2
投票

更换

'import android.widget.GridLayout;'

通过

'import android.support.v7.widget.GridLayout;'

这对我有用。希望这对你有用.......


1
投票

引起:java.lang.ClassCastException:android.support.v7.widget.GridLayout无法在devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)中强制转换为android.widget.GridLayout

=>从这一行开始,很明显你必须导入并引用android.widget.GridLayout,因为你已经在XML布局中使用了android.support.v7.widget.GridLayout。使用和引用布局和类文件中的任何一个。


0
投票

我有同样的问题,我只是去activity_main.xml并更换

android.support.v7.widget.GridLayout

对此:

android.widget.GridLayout

0
投票

我遇到了同样的问题。我通过使用包android.support.v7.widget.GridLayout中的GridLayout解决了它,因为我之前使用的android.widget.GridLayout package给了我错误。


0
投票

只需更改格式一点:androidx.gridlayout.widget.GridLayout newGrid =(androidx.gridlayout.widget.GridLayout)findViewById(R.id.yourGrid);

© www.soinside.com 2019 - 2024. All rights reserved.