对于在setOnClickListener之外实例化的对象,无法解析void类方法

时间:2018-04-10 09:56:42

标签: android

我开始学习如何开发Android应用程序,并开始只做一个简单的浸泡计时器。我有以下课程Clock

//-------------
//In Clock.java
//-------------
package greenthumbtack.teatimer;

public class Clock {
    private int min;
    private int sec;

    public Clock() {
        min = 0;
        sec = 0;
    }

    public Clock(int minutes, int seconds) {
        min = minutes;
        sec = seconds;
    }

    public final void setClock(int minutes, int seconds) {
        this.min = minutes;
        this.sec = seconds;
    }

    public String toStr() {
        return String.format("%d:%02d", min, sec);
    }

    public void tickDown() {
        this.sec -= 1;
        if (this.sec == 0 && this.min == 0) {
            this.sec = 0;
            this.min = 0;
        }
        else {
            if (this.sec < 0) {
                this.sec = 59;
                this.min -= 1;
            }
        }
    }
}

这对我来说似乎很好。然后在MainActivity.java我尝试执行以下操作:

public class MainActivity extends AppCompatActivity {

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

        Button blackTea = findViewById(R.id.blackTeaBtn);
        final TextView timer = findViewById(R.id.timerTxt);

        final Clock clock = new Clock(); //instantiate outside OnClickListener

        blackTea.setOnClickListener(new View.OnClickListener() {
            clock.setClock(5, 0); //setClock cannot be resolved
            @Override
            public void onClick(View v) {
                new CountDownTimer(1000 * 60 * 5, 1000) {
                    public void onTick(long millisUntilFinished) {

                        timer.setText(clock.toStr());
                        clock.tickDown(); //still fine
                    }
                    public void onFinish() {
                        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
                        toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,1000);
                        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        v.vibrate(1000);

                    }
                }.start();
            }
        });

但是,如果我在clock之外实例化OnClickListener,则无法解析其方法。我想在这里实例化它,因为我有多个按钮可以将clock设置为不同的时间,并认为只有一个Clock对象会更好。我注意到在onTick()中找到tickDown()并且工作正常。我发现我可以将时间设置为clock的唯一方法就是在OnClickListener内实例化它:

public class MainActivity extends AppCompatActivity {

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

        Button blackTea = findViewById(R.id.blackTeaBtn);
        Button greenTea = findViewById(R.id.greenTeaBtn);
        final TextView timer = findViewById(R.id.timerTxt);

        blackTea.setOnClickListener(new View.OnClickListener() {
            Clock clock = new Clock(5, 10);
            @Override
            public void onClick(View v) {
                new CountDownTimer(1000 * 60 * 5, 1000) {
                    public void onTick(long millisUntilFinished) {

                        timer.setText(clock.toStr());
                        clock.tickDown();
                    }
                    public void onFinish() {
                        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
                        toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,1000);
                        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        v.vibrate(1000);

                    }
                }.start();
            }
        });

如果我无法在OnClickListener之外创建此对象,然后访问public void之外的onTick方法以在对象上设置不同的时间,我做错了什么?

4 个答案:

答案 0 :(得分:3)

试试这个:

public class MainActivity extends AppCompatActivity {
    Button blackTea;
    TextView timer;
    Clock clock;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         blackTea = findViewById(R.id.blackTeaBtn);
         timer = findViewById(R.id.timerTxt);

         clock = new Clock(); 
        blackTea.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            clock.setClock(5, 0);
                new CountDownTimer(1000 * 60 * 5, 1000) {
                    public void onTick(long millisUntilFinished) {

                        timer.setText(clock.toStr());
                        clock.tickDown(); //still fine
                    }
                    public void onFinish() {
                        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
                        toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,1000);
                        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        v.vibrate(1000);

                    }
                }.start();
            }
        });

答案 1 :(得分:1)

尝试此操作在 onCreate() 方法

之外声明您的变量全局
 public class Clock {
    private int min;
    private int sec;

    public Clock() {
        min = 0;
        sec = 0;
    }

    public Clock(int minutes, int seconds) {
        min = minutes;
        sec = seconds;
    }

    public  void setClock(int minutes, int seconds) {
        this.min = minutes;
        this.sec = seconds;
    }

    public String toStr() {
        return String.format("%d:%02d", min, sec);
    }

    public void tickDown() {
        this.sec -= 1;
        if (this.sec == 0 && this.min == 0) {
            this.sec = 0;
            this.min = 0;
        }
        else {
            if (this.sec < 0) {
                this.sec = 59;
                this.min -= 1;
            }
        }
    }
}

活性

public class MainActivity extends AppCompatActivity {

    TextView blackTea;
    TextView timer;
    Clock clock;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        blackTea = findViewById(R.id.blackTeaBtn);
        timer = findViewById(R.id.timerTxt);

        clock = new Clock();
        blackTea.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                clock.setClock(5, 0);
                new CountDownTimer(1000 * 60 * 5, 1000) {
                    public void onTick(long millisUntilFinished) {


                        timer.setText(clock.toStr());
                        clock.tickDown(); //still fine
                    }
                    @SuppressLint("MissingPermission")
                    public void onFinish() {
                        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
                        toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,1000);
                        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        v.vibrate(1000);

                    }
                }.start();
            }
        });
    }

}

答案 2 :(得分:0)

你必须把这一行:

ElementName

到onClick()方法内部。像这样:

<Window.Resources>
    <DataTemplate x:Key="WarningIconImageTemplate">
        <StackPanel>
            <TextBlock Text="Some Text"></TextBlock>
            <Image Source="{Binding DataContext.WarningImageSource,ElementName=Main}" ></Image>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

答案 3 :(得分:0)

用于实例化时钟对象的代码中的错误位置。

请记住,您应该在 .setOnClickListener 方法之前实例化对象:

...
clock = new Clock();
clock.setClock(5, 0);
blackTea.setOnClickListener(new View.OnClickListener() {
...

OR OnClick 方法

...
blackTea.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   clock.setClock(5, 0);
...