【问题标题】:Error on a Specific Line Number - Android Studio特定行号上的错误 - Android Studio
【发布时间】:2020-05-21 01:21:51
【问题描述】:

无论我如何更改该特定行,即使我没有在其中添加任何内容,或者将其作为注释,或者将代码全部删除,错误消息都会一直告诉我该行导致错误。我什至尝试删除该类并将其全部复制到一个新的类中,但相同的行号仍然是一个错误。

错误行 (109) 突出显示的代码:

package com.example.president;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Game extends AppCompatActivity implements View.OnClickListener {


    private Manager gManager;
    Player p1,p2,p3;
    private ImageView[] hand;
    private ImageView[] curr;
    private ImageView[] next= new ImageView[3];
    private int[] turn = {0, 1, 2};
    private int cthrow;
    public int[] cards =
            {
                    R.drawable.c3,
                    R.drawable.c4,
                    R.drawable.c5,
                    R.drawable.c6,
                    R.drawable.c7,
                    R.drawable.c8,
                    R.drawable.c9,
                    R.drawable.c10,
                    R.drawable.c11,
                    R.drawable.c12,
                    R.drawable.c13,
                    R.drawable.c1,
                    R.drawable.c2,
                    R.drawable.h3,
                    R.drawable.h4,
                    R.drawable.h5,
                    R.drawable.h6,
                    R.drawable.h7,
                    R.drawable.h8,
                    R.drawable.h9,
                    R.drawable.h10,
                    R.drawable.h11,
                    R.drawable.h12,
                    R.drawable.h13,
                    R.drawable.h1,
                    R.drawable.h2,
                    R.drawable.s3,
                    R.drawable.s4,
                    R.drawable.s5,
                    R.drawable.s6,
                    R.drawable.s7,
                    R.drawable.s8,
                    R.drawable.s9,
                    R.drawable.s10,
                    R.drawable.s11,
                    R.drawable.s12,
                    R.drawable.s13,
                    R.drawable.s1,
                    R.drawable.s2,
                    R.drawable.d3,
                    R.drawable.d4,
                    R.drawable.d5,
                    R.drawable.d6,
                    R.drawable.d7,
                    R.drawable.d8,
                    R.drawable.d9,
                    R.drawable.d10,
                    R.drawable.d11,
                    R.drawable.d12,
                    R.drawable.d13,
                    R.drawable.d1,
                    R.drawable.d2,
                    R.drawable.j1,
                    R.drawable.j2
            };

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

        p1 = new Player("p1");
        p2 = new Player("p2");
        p3 = new Player("p3");

        this.hand = new ImageView[18];
        String str;
        int resId;
        int i;
        for (i=0;i<hand.length;i++)
        {
            str = "card"+i;
            resId = getResources().getIdentifier(str, "id", getPackageName());
            hand[i]= (ImageView)findViewById(resId);
            hand[i].setOnClickListener(this);
        }
        for (i=0; i<4; i++)
        {
            str="board"+i;
            resId = getResources().getIdentifier(str, "id", getPackageName());
            curr[i]= (ImageView)findViewById(resId);
            curr[i].setOnClickListener(this);
        }

        this.gManager = new Manager(this, p1, p2, p3);

        this.gManager.handingDeck(p1, p2, p3);
[[[LINE 109]]]
        startGame(p1, p2, p3);

    }

    public void startGame(Player p1, Player p2, Player p3) {

        Player p=p1;
        int i;
        for (i=0; i < 18; i++) {
            hand[i].setImageResource(cards[p.getHand().get(i).getIndex()]);
        }
        String text = p1.getHand().toString();
        TextView change = (TextView)findViewById(R.id.textView);
        change.setText(text);
    }

    @Override
    public void onClick(View v) {
        int i, cnum=0, t=0, resId;
        boolean found = false;
        Player p=p1;
        cthrow=1;
        for (i = 0; i < 18 && (!(found)); i++)
        {
            if (v.getId() == hand[i].getId())
            {
                String str="card"+cnum;
                resId=getResources().getIdentifier(str, "id", getPackageName());
                next[turn[t]]= (ImageView)findViewById(resId);
                found=true;
                curr[cthrow].setImageResource(cards[p.getHand().get(i).getIndex()]);
                p.getHand().remove(i);
                next[turn[t]].setVisibility(View.INVISIBLE);
                if(cnum<10)
                    cnum=18-cnum;
                else
                    cnum=18-cnum+1;
                cthrow++;
            }
        }
    }
}

如您所见,该行甚至是空的。

【问题讨论】:

  • 只是为了好奇,你为什么不粘贴你的logcat?
  • 你没有初始化你的 curr。试试this.curr = new ImageView[4]; 根据需要更改大小
  • 我为什么要发布它?
  • @Md.Asaduzzaman 大声笑你说得对,谢谢你仍然没有解决它
  • 你为什么不debug 代码并检查导致问题的原因?这样做很容易,而不是问一个愚蠢的问题。希望你理解

标签: javascript java android android-studio


【解决方案1】:

您以错误的方式初始化数组,使用 new 它将初始化数组并为该数组创建内存。

-- 下面是初始化和向数组添加值的正确方法。

public int[] cards = new int[]{ add your Drawbles here };

【讨论】:

  • 你没有初始化 curr 数组 ..initialize 先给它加值
  • 我知道了,现在已经初始化了
  • “已经收到通知,仍然没有解决错误”——正如我之前所说的
【解决方案2】:

正如 cmets 中所指出的,错误实际上发生在这里:

HERE>> curr[i]= (ImageView)findViewById(resId);
curr[i].setOnClickListener(this);

但它会发生在您使用curr 的任何行,因为它没有被初始化。

针对您的具体错误: 如果即使您有意更改代码,错误行也不会更改,则唯一合理的答案只有一个。实际运行的代码是的。所以你需要刷新它。如何?这取决于您在何处/如何运行它,但直接的方法是逐步执行此步骤(测试是否同时发生变化):

  • 重建您的项目(使用您的 ide 重建按钮或通过命令行)
  • 清理构建您的项目(ide 或 cmd 同上)
  • 重新部署您的应用程序(如果是 Android 应用程序,请将其“重新安装”在您正在测试的设备上;模拟器或手机上)
  • 卸载并删除当前正在运行的应用程序(并重复第 2 点和第 3 点)
  • 重新启动您的测试设备(模拟器/手机并重复第 2 点和第 3 点)
  • 重启你的IDE(如果你不确定它的所有进程都重启了,重启你的电脑)
  • 删除您的 ide 可能存储的任何构建中间/缓存文件(并重复 2 和 3)
  • 没有任何效果。尝试上述解决方案的不同组合
  • 如果您在这里,请开始思考最愚蠢的事情并发挥创造力,例如:
    • 我在安装应用程序吗?
    • 我是否保存了修改后的文件?
    • 电话接通了吗?
    • 我是否修改了正确项目中的文件?
    • ...

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2015-03-24
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多