简体   繁体   English

从未使用方法“ exampleMethod”

[英]Method “exampleMethod” is never used

I am writing a Rock Paper Scissors program in android studio, here's what I have so far: (everything necessary is imported) 我正在android studio中写一个Rock Paper Scissors程序,这是我到目前为止所做的:(所有必需的东西都是导入的)

    Random compans = new Random();
    int low = 1;
    int high = 3;

    int RPS = compans.nextInt(high-low) + low;

public void rock (View view) {
    Button Rock = (Button) findViewById(R.id.rock);
    Rock.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            /* all capitals = user answer */
            if (RPS == 1) {
                startActivity(new Intent(MainActivity.this,Tie.class));
                //ROCK-rock tie
                }
            else if (RPS == 2) {
                //ROCK-paper lose
                startActivity(new Intent(MainActivity.this,Lose.class));
            }
            else if (RPS == 3) {
                //ROCK-scissors win
                startActivity(new Intent(MainActivity.this,Win.class));
            }
            }
        });
        }
public void paper (View view) {
    Button paper = (Button) findViewById(R.id.paper);
    paper.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (RPS == 1) {
                //PAPER-rock win
                startActivity(new Intent(MainActivity.this,Win.class));
            } else if (RPS == 2) {
                //PAPER-paper tie
                startActivity(new Intent(MainActivity.this,Tie.class));
            } else if (RPS == 3) {
                //PAPER-scissors lose
                startActivity(new Intent(MainActivity.this,Lose.class));
            }
        }
    });
}
public void scissors (View v) {
    Button scissors = (Button) findViewById(R.id.scissors);
    scissors.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                if (RPS == 1) {
                    //SCISSORS-rock lose
                    startActivity(new Intent(MainActivity.this, Lose.class));
                } else if (RPS == 2) {
                    //SCISSORS-paper win
                    startActivity(new Intent(MainActivity.this, Win.class));
                } else if (RPS == 3) {
                    //SCISSORS-scissors tie
                    startActivity(new Intent(MainActivity.this, Tie.class));
                }
            }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

When I finished, I noticed none of the "rock" "paper" or "scissor" methods were used. 当我完成时,我注意到没有使用“摇滚”“纸”或“剪刀”方法。 When I ran it on my phone, my buttons didn't do anything, same goes for the emulator. 当我在手机上运行时,我的按钮没有做任何事情,仿真器也是如此。

The message I get (Don't have enough rep to upload) 我得到的消息 (没有足够的代表上传)

An explanation and solution of the message would be nice, as I'm still a beginner at this. 对该消息的解释和解决方案将是不错的,因为我仍然是对此的初学者。 Any help would be appreciated, thanks! 任何帮助将不胜感激,谢谢!

The message means: you did not call any of those method when running program. 该消息表示:在运行程序时,您没有调用任何这些方法。

Naive solution: Move all content of 3 methods: rock,... to onCreate() : 天真的解决方案:移动3种方法的所有内容:rock,... to onCreate()

onCreate(...){
   ...
   setContentView(...);
   //----Example for rock button
   Button Rock = (Button) findViewById(R.id.rock);
   Rock.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        /* all capitals = user answer */
        if (RPS == 1) {
            startActivity(new Intent(MainActivity.this,Tie.class));
            //ROCK-rock tie
            }
        else if (RPS == 2) {
            //ROCK-paper lose
            startActivity(new Intent(MainActivity.this,Lose.class));
        }
        else if (RPS == 3) {
            //ROCK-scissors win
            startActivity(new Intent(MainActivity.this,Win.class));
        }
        }
    });
    }

//Do the same for other buttons here

}//end of onCreate

IDE is telling you that those methods were not being called. IDE告诉您未调用这些方法。 You didnt post the onCreate contents, you would want to move the button.setOnClick.. over in the onCreate. 你没有发布onCreate内容,你想在onCreate中移动button.setOnClick ..。

On a side note instead of hooking up onclick like that you could do it on your layout file. 在侧面注释而不是挂钩onclick就像你可以在你的布局文件上做。 For example 例如

<Button
   android:id="@+id/rock"
   android:onclick="rock"
   android:text="Rock" />

Here when rock button is clicked it calls "rock" button in your activity. 在点击摇滚按钮时,它会在您的活动中调用“摇滚”按钮。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM