[英]Chronometer Cannot Resolve Method
I am having an issue in setting up my Chronometer. 我在设置计时器时遇到问题。 The Java file shows an error at "updateTimerText" stating Cannot resolve method "updateTimerText (java.lang.string) How do I solve this? Everything is Fine on the fragment I am trying to cast this task to it is just the Chronometer.java file. If you need any other information or code, let me know. Thanks in advance.
Java文件在“ updateTimerText”处显示错误,指出无法解决方法“ updateTimerText(java.lang.string)”。如何解决此问题?片段上的一切都很好,我正在尝试将此任务转换为Chronometer.java文件。如果您需要任何其他信息或代码,请告诉我,谢谢。
package com.example.platinumirish.runassistwithdrawer;
import android.content.Context;
import runassist.demo.fragments.MainFragment;
/**
* Created by Platinum Irish on 22/06/2016.
*/
public class Chronometer implements Runnable {
public static final long MILLIS_TO_MINUTES = 60000;
public static final long MILLIS_TO_HOURS = 3600000;
private Context mContext;
private long mStartTime;
private boolean mIsRunning;
public Chronometer(Context context){
mContext = context;
}
public void start() {
mStartTime = System.currentTimeMillis();
mIsRunning = true;
}
public void stop() {
mIsRunning = false;
}
@Override
public void run() {
while(mIsRunning) {
long since = System.currentTimeMillis() - mStartTime;
int seconds = (int) (since / 1000 % 60);
int minutes = (int)(((since / MILLIS_TO_MINUTES)) % 60);
int hours = (int)((since / (MILLIS_TO_HOURS)) % 24);
int millis = (int) since % 1000;
MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
"%02d;%02d;%02d;%03d", hours, minutes, seconds, millis
));
}
}
}
Here is my Main Fragment too if it will help in any way, shape or form. 这也是我的主要片段,如果它可以以任何方式,形状或形式提供帮助。
package runassist.demo.fragments;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import com.example.platinumirish.runassistwithdrawer.R;
public class MainFragment extends Fragment{
private TextView mTvTime;
private Button mBtnStart;
private Button mBtnStop;
private Context mContext;
private Chronometer mChronometer;
private Thread mThreadChrono;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTvTime = (TextView) rootView.findViewById(R.id.chronometer);
mBtnStart = (Button) rootView.findViewById(R.id.start_button);
mBtnStop = (Button) rootView.findViewById(R.id.stop_button);
mBtnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mChronometer == null) {
mChronometer = new Chronometer(mContext);
mThreadChrono = new Thread((Runnable) mChronometer);
mThreadChrono.start();
mChronometer.start();
}
}
});
mBtnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mChronometer !=null) {
mChronometer.stop();
mThreadChrono.interrupt();
mThreadChrono = null;
mChronometer = null;
}
}
});
return rootView;
}
public void updateTimerText(final String time) {
runOnUiThread(new Runnable() {
@Override
public void run () {
mTvTime.setText(time);
}
});
}
private void runOnUiThread(Runnable runnable) {
}
} }
((MainFragment) mContext).updateTimerText(...)
this is the way you want to call it. ((MainFragment) mContext).updateTimerText(...)
这是您要调用的方式。 Check the squares around mContext. 检查mContext周围的正方形。 Also, your method
updateTimerText
returns void
so this line: 另外,您的方法
updateTimerText
返回void
因此此行:
MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
"%02d;%02d;%02d;%03d", hours, minutes, seconds, millis))
will raise another error. 会引发另一个错误。 It should simply be:
它应该只是:
((MainFragment) mContext).updateTimerText(...)
I can't help it, I have to suggest to use a different approach, first of all it's a bad idea to keep a reference to the context in a Runnable class. 我无能为力,我不得不建议使用其他方法,首先在Runnable类中保留对上下文的引用是一个坏主意。
Second of all, it's bad to cast Context
to either Activity
or Fragment
, it will reduce the code reusability and cause unexpected crashes if the dev is not aware of this casts. 其次,将
Context
强制转换为Activity
或Fragment
是很不好的,如果开发人员不了解这种强制转换,它将降低代码的可重用性并导致意外崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.