简体   繁体   English

在OnCreate中定义按钮-无法解析方法中的符号

[英]Defining Button in OnCreate - Cannot resolve symbol in method

I'm a beginner as you'll see. 如您所见,我是个初学者。 I can't see where I can legally put the Button statements and the setText statements so this will compile. 我看不到在哪里可以合法地放置Button语句和setText语句,以便可以进行编译。 If I move one the other doesn't work and vice versa. 如果我一个移动,另一个则不起作用,反之亦然。 I understand that the OnCreate is protected and so the "buttonOne" won't be passed to the playPhrases method but I've been changing thing around for a while now and nothing works. 我知道OnCreate受保护,因此“ buttonOne”不会传递给playPhrases方法,但是我已经改变了一段时间了,但没有任何效果。 Simple explanations please. 请简单说明。

    package com.example.android.languageapp;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.button;

public class MainActivity extends AppCompatActivity {

    MediaPlayer myMediaPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myMediaPlayer = MediaPlayer.create(this, R.raw.hello);

        String[] phrases = getResources().getStringArray(R.array.phrase);



        Button buttonOne = (Button) findViewById(R.id.button1);
        Button buttonTwo = (Button) findViewById(R.id.button2);
        Button buttonThree = (Button) findViewById(R.id.button3);
        Button buttonFour = (Button) findViewById(R.id.button4);
        Button buttonFive = (Button) findViewById(R.id.button5);
        Button buttonSix = (Button) findViewById(R.id.button6);
        Button buttonSeven = (Button) findViewById(R.id.button7);
        Button buttonEight = (Button) findViewById(R.id.button8);

        buttonOne.setText(phrases[0]);
        buttonTwo.setText(phrases[1]);
        buttonThree.setText(phrases[2]);
        buttonFour.setText(phrases[3]);
        buttonFive.setText(phrases[4]);
        buttonSix.setText(phrases[5]);
        buttonSeven.setText(phrases[6]);
        buttonEight.setText(phrases[7]);
    }


    public void playPhrases(View clickedButton) {

        int id = clickedButton.getId();

        Log.i("Button id", "" + id);

        if (id == buttonOne.getId()) {

            myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);

            myMediaPlayer.start();
        }


    }



}

Thanks for your help 谢谢你的帮助

You use just use id in playPhrases click listener like this, 您只需要在playPhrases点击监听器中使用id ,就像这样,

public void playPhrases(View clickedButton) {

    int id = clickedButton.getId();

    Log.i("Button id", "" + id);

    switch (id) {
        case R.id.R.id.button1:
            // Button 1 operation
            myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);

            myMediaPlayer.start();
            break;
        case R.id.R.id.button2:
            // Button 2 operation
            break;
        case R.id.R.id.button3:
            // Button 3 operation
            break;
    }
    // We dont need button object
    //if (id == buttonOne.getId()) {
    //
    //    myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
    //
    //    myMediaPlayer.start();
    //}


}

Declare your Buttons outside the onCreate method and then attach them to views in your onCreate . 在onCreate方法之外声明您的Button,然后将它们附加到onCreate视图中。 That way you can access them later anywhere in your class. 这样,您以后就可以在班上的任何地方访问它们。 I have shown it with 3 buttons as example but you can do it the same way for all. 我以3个按钮为例进行了显示,但是您可以对所有对象进行相同的操作。

These button becomes Data members of the class or Global variables rather than Local Variables of onCreate method. 这些按钮成为类的数据成员或全局变量,而不是onCreate方法的局部变量。 To understand more about Global Vairables visit Here 要了解更多有关全球可折叠产品的信息,请访问此处

public class MainActivity extends AppCompatActivity {
MediaPlayer myMediaPlayer;
Button buttonOne ,buttonTwo,buttonThree ; // Do it this way first for buttons to create reference

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMediaPlayer = MediaPlayer.create(this, R.raw.hello);

    String[] phrases = getResources().getStringArray(R.array.phrase);


    // Access like this 
    buttonOne = (Button) findViewById(R.id.button1);
    buttonTwo = (Button) findViewById(R.id.button2);
    buttonThree = (Button) findViewById(R.id.button3);
    Button buttonFour = (Button) findViewById(R.id.button4);
    Button buttonFive = (Button) findViewById(R.id.button5);
    Button buttonSix = (Button) findViewById(R.id.button6);
    Button buttonSeven = (Button) findViewById(R.id.button7);
    Button buttonEight = (Button) findViewById(R.id.button8);

    buttonOne.setText(phrases[0]);
    buttonTwo.setText(phrases[1]);
    buttonThree.setText(phrases[2]);
    buttonFour.setText(phrases[3]);
    buttonFive.setText(phrases[4]);
    buttonSix.setText(phrases[5]);
    buttonSeven.setText(phrases[6]);
    buttonEight.setText(phrases[7]);
}


public void playPhrases(View clickedButton) {

    int id = clickedButton.getId();

    Log.i("Button id", "" + id);

    if (id == buttonOne.getId()) {

        myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);

        myMediaPlayer.start();
    }


}



}

define Button Variables globally try this 全局定义按钮变量试试看

public class MainActivity extends AppCompatActivity {

MediaPlayer myMediaPlayer;
Button buttonOne;
Button buttonTwo ;
Button buttonThree ;
Button buttonFour;
Button buttonFive;
Button buttonSix ;
Button buttonSeven ;
Button buttonEight;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMediaPlayer = MediaPlayer.create(this, R.raw.hello);

    String[] phrases = getResources().getStringArray(R.array.phrase);



     buttonOne = (Button) findViewById(R.id.button1);
     buttonTwo = (Button) findViewById(R.id.button2);
     buttonThree = (Button) findViewById(R.id.button3);
     buttonFour = (Button) findViewById(R.id.button4);
     buttonFive = (Button) findViewById(R.id.button5);
     buttonSix = (Button) findViewById(R.id.button6);
     buttonSeven = (Button) findViewById(R.id.button7);
     buttonEight = (Button) findViewById(R.id.button8);

    buttonOne.setText(phrases[0]);
    buttonTwo.setText(phrases[1]);
    buttonThree.setText(phrases[2]);
    buttonFour.setText(phrases[3]);
    buttonFive.setText(phrases[4]);
    buttonSix.setText(phrases[5]);
    buttonSeven.setText(phrases[6]);
    buttonEight.setText(phrases[7]);
}


public void playPhrases(View clickedButton) {

    int id = clickedButton.getId();

    Log.i("Button id", "" + id);

    if (id == buttonOne.getId()) {

        myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);

        myMediaPlayer.start();
    }


}

} }

You have to move the declaration of your buttons variables out of the OnCreate method to declare it globally. 您必须将按钮变量的声明移出OnCreate方法,才能进行全局声明。 Button buttonOne; 按钮buttonOne; Must be in the MainActivity class if you declare it in the method it can't be used in another one; 如果您在无法用于其他方法的方法中声明它,则必须在MainActivity类中; Then you can still do : buttonOne = (Button) findViewById(R.id.button1); 然后,您仍然可以:buttonOne =(Button)findViewById(R.id.button1); in OnCreate 在OnCreate中

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

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