java - 错误: android. widget.LinearLayout 无法转换为 android.widget.ImageView

标签 java android android-layout logcat

已经有其他人遇到类似的问题,但我无法理解答案,并且无法添加评论来接收 更多信息。所以请帮助我!

这是我的主要 JavaClass

package ch.androidnewcomer.muckenfangfinal;


import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Date;
import java.util.Random;

public class GameActivity extends Activity implements View.OnClickListener,         Runnable{

public static final int DELAY_MILLIS = 1000;
public static final int ZEITSCHEIBEN = 60;
public float massstab;
private int runde;
private int punkte;
private int muecken;
private int gefangeneMuecken;
private int zeit;
private static final long HOECHSTALTER_MS = 2000;
private Random zufallsgenerator = new Random();
private ViewGroup spielbereich;
private Handler handler = new Handler();
private boolean spielLaeuft;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    massstab = getResources().getDisplayMetrics().density;
    spielbereich = (ViewGroup)findViewById(R.id.spielbereich);
    spielStarten();

}




private void spielStarten() {
    spielLaeuft = true;
    runde = 0;
    punkte = 0;
    starteRunde();
}



private void bildschirmAktualisieren() {
    TextView tvPunkte = (TextView)findViewById(R.id.points);
    tvPunkte.setText(Integer.toString(punkte));

    TextView tvRunde = (TextView)findViewById(R.id.round);
    tvRunde.setText(Integer.toString(runde));

    TextView tvTreffer = (TextView)findViewById(R.id.hits);
    tvTreffer.setText(Integer.toString(gefangeneMuecken));

    TextView tvZeit = (TextView)findViewById(R.id.time);
    tvZeit.setText(Integer.toString(zeit/(1000/DELAY_MILLIS)));

    FrameLayout flTreffer = (FrameLayout)findViewById(R.id.bar_hits);
    FrameLayout flZeit = (FrameLayout)findViewById(R.id.bar_time);


    ViewGroup.LayoutParams lpTreffer = flTreffer.getLayoutParams();
    lpTreffer.width = Math.round( massstab * 300 * Math.min(                        gefangeneMuecken,muecken) / muecken);

    ViewGroup.LayoutParams lpZeit = flZeit.getLayoutParams();
    lpZeit.width = Math.round( massstab * zeit *300 / ZEITSCHEIBEN);


}





private void zeitHerunterzaehlen() {
    zeit = zeit - 1;
    if(zeit % (1000/DELAY_MILLIS) == 0) {
        float zufallszahl = zufallsgenerator.nextFloat();
        double wahrscheinlichkeit = muecken * 1.5;
        if (wahrscheinlichkeit > 1) {
            eineMueckeAnzeigen();
            if (zufallszahl < wahrscheinlichkeit - 1) {
                eineMueckeAnzeigen();
            }
        } else {
            if (zufallszahl < wahrscheinlichkeit) {
                eineMueckeAnzeigen();
            }
        }
    }

    mueckenVerschwinden();
    bildschirmAktualisieren();
    if(!pruefeSpielende()) {
        if(!pruefeRundenende()) {
            handler.postDelayed(this, DELAY_MILLIS);
        }
    }
}



private boolean pruefeRundenende()  {
    if (gefangeneMuecken >= muecken) {
        starteRunde();
        return true;
    }
    return false;
}


private void starteRunde() {
    runde = runde +1;
    muecken = runde *10;
    gefangeneMuecken = 0;
    zeit = ZEITSCHEIBEN;
    bildschirmAktualisieren();
    handler.postDelayed(this, DELAY_MILLIS);

}

private boolean pruefeSpielende() {
    if(zeit == 0 && gefangeneMuecken < muecken) {
        gameOver();
        return  true;
    }
    return false;
}













private void gameOver() {
    Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    dialog.setContentView(R.layout.gameover);
    dialog.show();
    spielLaeuft = false;
}

private void mueckenVerschwinden() {
    int nummer=0;
    while (nummer < spielbereich.getChildCount()) {
        ImageView muecke = (ImageView)spielbereich.getChildAt(nummer);
        Date geburtsdatum = (Date) muecke.getTag(R.id.geburtsdatum);
        long alter = (new Date()).getTime() - geburtsdatum.getTime();
        if(alter > HOECHSTALTER_MS) {
            spielbereich.removeView(muecke);
        }else {
            nummer++;
        }
    }
}




private void eineMueckeAnzeigen() {
    int breite = spielbereich.getWidth();
    int hoehe = spielbereich.getHeight();
    int muecke_breite = (int) Math.round(massstab* 60);
    int muecke_hoehe = (int) Math.round(massstab*49);
    int links = zufallsgenerator.nextInt(breite - muecke_breite);
    int oben = zufallsgenerator.nextInt(hoehe - muecke_hoehe);

    ImageView muecke = new ImageView(this);
    muecke.setImageResource(R.drawable.muecke);
    muecke.setOnClickListener(this);
    muecke.setTag(R.id.geburtsdatum, new Date());

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(muecke_breite, muecke_hoehe);
    params.leftMargin = links;
    params.topMargin = oben;
    params.gravity = Gravity.TOP + Gravity.LEFT;

    spielbereich.addView(muecke,params);

}





@Override
public void onClick(View muecke) {
    gefangeneMuecken++;
    punkte += 100;
    bildschirmAktualisieren();
    spielbereich.removeView(muecke);
}

@Override
public void run() {
    zeitHerunterzaehlen();
}

}

我试图在其中显示“muecke - 蚊子”的 FrameLayout 被称为“spielbereich”,意思与 Playground 一样。

这是 LogCat

12-28 16:19:26.449 5302-5302/ch.androidnewcomer.muckenfangfinal       E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: ch.androidnewcomer.muckenfangfinal, PID: 5302
                                                                                  java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
                                                                                      at        ch.androidnewcomer.muckenfangfinal.GameActivity.mueckenVerschwinden(GameActivity.java:168)
                                                                                  at ch.androidnewcomer.muckenfangfinal.GameActivity.zeitHerunterzaehlen(GameActivity.java:104)
                                                                                  at ch.androidnewcomer.muckenfangfinal.GameActivity.run(GameActivity.java:218)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

这是我的 xmlLayout:

<?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"
tools:context="ch.androidnewcomer.muckenfangfinal.GameActivity">


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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="61dp">

        <TextView
            android:id="@+id/round"
            android:layout_width="136dp"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="21sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/points"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="TextView"
            android:textSize="21sp"
            android:textStyle="bold" />


    </FrameLayout>

    <FrameLayout
        android:id="@+id/spielbereich"
        android:layout_width="match_parent"
        android:layout_height="332dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/hintergrundlinearlayout"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="35dp">

                <FrameLayout
                    android:id="@+id/bar_hits"
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:background="@android:color/holo_orange_dark">

                </FrameLayout>

                <TextView
                    android:id="@+id/hits"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text="@string/hits"
                    android:textSize="15sp"
                    android:textStyle="bold" />
            </FrameLayout>


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <FrameLayout
                    android:id="@+id/bar_time"
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:background="@android:color/holo_orange_light">

                </FrameLayout>

                <TextView
                    android:id="@+id/time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text="@string/time" />
            </FrameLayout>
        </LinearLayout>

    </FrameLayout>

</LinearLayout>

如果您需要更多信息,请告诉我。 如果您能准确地告诉我如何更改代码,那就太好了,因为我真的是一个初学者。

最佳答案

您正在将线性布局转换为 ImageView 。您正在尝试获取框架布局的子级,该子级是线性布局,但您将其转换为 ImageView 。这就是发生此错误的原因。将其转换为线性布局将解决此错误。

关于java - 错误: android. widget.LinearLayout 无法转换为 android.widget.ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48010995/

相关文章:

java - 在android中将16位音频存储在8位字节数组上

java - 如何在不同的时间启动多个移动的 Sprite 对象?

android - 如何使用线性布局将按钮设置到屏幕底部

安卓设计库 : SwipeRefreshLayout don't detect swipe over CollapsingToolbarLayout

java - 将我的硬盘索引到 solr

java - 如何右对齐单元格中的 RichTextString?

java - 我正在转换日期,但它在月份中多加了一个点如何处理

java - 四元数相乘时结果无效,不知道为什么

android - 如何以相同的 Intent 从相机获取全尺寸图片和缩略图

Android RelativeLayout 在另一个元素边框上对齐元素