简体   繁体   English

为什么我无法使用“setImageRecource”来更改 Android Studio 中 ImageView 使用的可绘制对象?

[英]Why am I unable to use "setImageRecource" to change the drawable used by the ImageView in Android Studio?

I'm learning how to develop apps for Android, I´m familiar with Java through school courses.我正在学习如何为 Android 开发应用程序,我通过学校课程熟悉 Java。 Currently, I m developing a small TicTacToe app for exercise purpose and I´m trying to show a cross instead of a circle but Android Studio cant compile setImageRecource` command even though in every damn online forum this method is recommended.目前,我m developing a small TicTacToe app for exercise purpose and I´m trying to show a cross instead of a circle but Android Studio cant compile setImageRecource` 命令,即使在每个该死的在线论坛中都推荐使用这种方法。

I've already tried things like set setBackgroundRecource but all these things doesn't fulfill my purpose...我已经尝试过 set setBackgroundRecource东西,但所有这些东西都不能满足我的目的......

The Java Code: Java代码:

 public void click(View pView){
    if(getActivePlayer()== 1){
        if(s1.getSymbol().equals("cross")){
            pView.setImageResource(R.drawable.circle); //claims cannot Resolve method 'setIMageRecource(int)'
        }
        else{
            pView.setImageResource(R.drawable.cross); //claims cannot Resolve method 'setIMageRecource(int)'
        }
    }
    else if(getActivePlayer()==2){
        if(s2.getSymbol().equals("circle")){
            pView.setImageResource(R.drawable.circle); //claims cannot Resolve method 'setIMageRecource(int)'
        }
        else{
            pView.setImageResource(R.drawable.cross); //claims cannot Resolve method 'setIMageRecource(int)'
        }
    }
    pView.setY(-1000);
    pView.setAlpha(1);
    pView.animate().translationYBy(1000).setDuration(1000);
    if (getActivePlayer() == 1){
        setActivePlayer(2);
        String checker = "aktive player ist gerade" + getActivePlayer();
        Log.i("INfo", checker);
    }
    else{
        setActivePlayer(1);
        String checker = "aktive player ist gerade" + getActivePlayer();
        Log.i("INfo", checker);
    }

}

In XML:在 XML 中:

 <ImageView
            android:id="@+id/field4"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:onClick="click"
            app:srcCompat="@drawable/circle" />

You can use ImageView.setImageDrawable(Context impact.getDrawable(mContext, R.drawable.cross));您可以使用 ImageView.setImageDrawable(Context Impact.getDrawable(mContext, R.drawable.cross));

Here mContext is globally declared as your activity reference.这里 mContext 被全局声明为您的活动引用。

您需要像这样将ViewImageView

((ImageView) pView).setImageResource(R.drawable.cross);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法在 android studio 中使用 FirebaseDatabase - why i am not able to use FirebaseDatabase in android studio 为什么我不能使用我的paintComponent? - Why am I unable to use my paintComponent? Android:无法更改Fragment中按钮的背景(可绘制) - Android: Unable to change background (drawable) of a button in Fragment 如何在 android 工作室中更改 imageview 上的文本 position? - How to change position of text on imageview in android studio? 为什么我在android studio中的代码中出现错误? - Why am i getting errors in my code in android studio? 为什么我无法使用准备好的语句来更新数据库? - Why am I unable to use prepared statements to update a database? Android:为什么我无法将图像上传到Web服务器? - Android: Why am I unable to upload an image to a web server? Android Studio-可绘制文件夹 - Android Studio - Drawable folders Android Studio:如何从用于设置按钮背景的可绘制对象中删除白色背景? - Android Studio: How do I remove the white background from my drawable, which is used to set the background of my button? 为什么在Android Studio中的SearchView和OnQueryTextListener上出现此错误? - Why am I getting this error in Android Studio on SearchView and OnQueryTextListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM