编辑:我需要更改几个变量的值,因为它们通过计时器运行几次.我需要通过计时器每次迭代不断更新值.我无法将值设置为final,因为这会阻止我更新值,但是我收到了我在下面的初始问题中描述的错误:
我以前写过以下内容:
我收到错误"不能引用在不同方法中定义的内部类中的非final变量".
这种情况发生在双重调用价格和价格调用priceObject上.你知道我为什么会遇到这个问题.我不明白为什么我需要最后的声明.此外,如果你能看到我想要做的是什么,我该怎么做才能解决这个问题.
public static void main(String args[]) {
int period = 2000;
int delay = 2000;
double lastPrice = 0;
Price priceObject = new Price();
double price = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
price = priceObject.getNextPrice(lastPrice);
System.out.println();
lastPrice = price;
}
}, delay, period);
}
Run Code Online (Sandbox Code Playgroud) 我需要一个"Runnable接受一个参数",虽然我知道这样的runnable并不存在.
这可能指出我的应用程序设计中的根本缺陷和/或疲惫的大脑中的心理障碍,所以我希望在这里找到关于如何完成以下内容的一些建议,而不违反基本的OO原则:
private Runnable mOneShotTask = new Runnable(String str) {
public void run(String str) {
someFunc(str);
}
};
Run Code Online (Sandbox Code Playgroud)
知道如何完成上述内容吗?
如何知道正在运行的代码是否在主线程(UI线程)上执行?
使用Swing我使用isEventDispatchThread方法...
我正在使用这个,从互联网上显示图像,但它抛出一个错误如下:
04-12 13:45:05.337:E/AndroidRuntime(27897):引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有原始线程创建视图层次结构可以触及其视图.
public class Order extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new DownloadFilesTask().execute();
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void result) {
}
@Override
protected Void doInBackground(Void... params) {
setContentView(R.layout.order);
ImageView imageView = (ImageView)findViewById(R.id.imgView);
imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));
return null;
}
}
private Drawable createDrawableFromURL(String urlString) {
Drawable image = null;
try {
URL url = new URL(urlString);
InputStream is = (InputStream)url.getContent();
image = Drawable.createFromStream(is, "src");
} catch (MalformedURLException …Run Code Online (Sandbox Code Playgroud)