姬長信(Redy)

Android – Gridlayout android.support.v7.widget.Grid…


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

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

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

activity_main





    



    

        
    

    


        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        

        
        

        


            

                

                
            

        
    
    


Logs

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)?

Gradle

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'

}

MainActivity.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