我想创建一个能够在所有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'
}
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 < 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);
}
}
});
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:2)
替换
'import android.widget.GridLayout;'
通过
'导入android.support.v7.widget.GridLayout;'
这对我有用。希望这对您有用.......
答案 2 :(得分:1)
引起:java.lang.ClassCastException: android.support.v7.widget.GridLayout无法强制转换为 android.widget.GridLayout at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)
=&GT;从这一行开始,很明显您必须导入并引用android.widget.GridLayout
,因为您已经在XML布局中使用了android.support.v7.widget.GridLayout
。在布局和类文件中使用和引用任何一个。
答案 3 :(得分:0)
添加您的依赖项
compile 'com.android.support:gridlayout-v7:26.1.0'
答案 4 :(得分:0)
我遇到了同样的问题,我只是去了activity_main.xml
并替换了
android.support.v7.widget.GridLayout
到此:
android.widget.GridLayout
答案 5 :(得分:0)
我遇到了同样的问题。我使用包android.support.v7.widget.GridLayout
中的GridLayout来解决它,因为我以前使用的是android.widget.GridLayout package
,这给了我错误。
答案 6 :(得分:0)
只需稍微更改一下格式: androidx.gridlayout.widget.GridLayout newGrid = (androidx.gridlayout.widget.GridLayout)findViewById(R.id.yourGrid);
答案 7 :(得分:0)
import androidx.gridlayout.widget.GridLayout
在您的 MainActiviy 工作表中执行此操作。
您应该编辑import.android.widget.GridLayout
为
import androidx.gridlayout.widget.GridLayout