我正在尝试多线程化我的代码,但是当我将OnClickListener
放入新的Thread
时,Eclipse中会出现此错误:
The method onClick(View) from the type new Thread(){} is never used locally
我的MainClass
代码:
public class MainClass extends Activity implements OnClickListener {
public float goldCount;
Button minionClick;
Button storeClick;
Button storeDismiss;
TextView textGoldCount;
ImageView spinningBars;
String textTotal;
private SharedPreferences prefs;
PopupWindow pw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.storemenu, null, false));
storeDismiss = (Button) pw.getContentView().findViewById(R.id.menudismissid);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.spinningbarsanimation);
rotation.setRepeatCount(Animation.INFINITE);
spinningBars.startAnimation(rotation);
prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);
goldCount = prefs.getFloat("goldCount", 0.0f);
minionClick = (Button) findViewById(R.id.minioncentreid);
storeClick = (Button) findViewById(R.id.storeimageid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
spinningBars = (ImageView) findViewById(R.id.spinningbarsid);
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);
minionClick.setOnClickListener(this);
storeClick.setOnClickListener(this);
storeDismiss.setOnClickListener(this);
}
@Override
public void onPause() {
super.onPause();
prefs.edit().putFloat("goldCount", goldCount).commit();
}
@Override
public void onResume() {
super.onResume();
goldCount = prefs.getFloat("goldCount", 0.0f);
t.start();
}
@Override
public void onStop() {
super.onStop();
prefs.edit().putFloat("goldCount", goldCount).commit();
Log.d(prefs.getFloat("goldCount", 0.0f) + "derprolw", "ejwfjbrea");
}
Thread t = new Thread() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.minioncentreid:
goldCount += 1.0;
prefs.edit().putFloat("goldCount", goldCount).commit();
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
case R.id.storeimageid:
LayoutInflater inflater = (LayoutInflater) MainClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.storemenu, null);
final PopupWindow pw = new PopupWindow(popupview, 300, 450);
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
storeDismiss = (Button) pw.getContentView().findViewById(R.id.menudismissid);
storeDismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
pw.showAsDropDown(storeClick, 0, 0);
break;
}
};
};
@Override
public void onClick(View v) {
}
}
Thread
位于Thread t = new Thread()
。有没有人知道为什么会这样,以及如何解决它?
答案 0 :(得分:0)
当您通过XML为视图定义单击侦听器时,它会在您的Activity中查找侦听器,而不是您可能创建的任何随机线程。您的体系结构需要更改,以便onClick位于Activity中,然后该方法使用该线程
答案 1 :(得分:0)
当您在OnClickListeners
Activity
中设置MainClass
时,请执行以下操作:
minionClick.setOnClickListener(this);
storeClick.setOnClickListener(this);
storeDismiss.setOnClickListener(this);
然后您将Activity
设置为侦听器而不是线程。点击发生时将调用的方法是Activity
类底部的方法:
@Override
public void onClick(View v) {
}
而不是Thread
内的那个。我认为令你困惑的部分如下:
当你做这样的事情时:
Thread thread = new Thread() {
};
然后,您实际上正在创建一个全新的类 - 称为匿名类 - 它是Thread
的子类。因此,如果您在其中定义一个方法,那么它不属于Activity
,它实际上是一个完全不同的类。
另一件需要考虑的事情是,无论如何,你要做的事情并不是一个好主意。你应该做的是在OnClickListener
中启动线程,而不是单独处理Thread
中的点击。尝试这样的事情:
@Override
public void onClick(View v) {
Thread thread = new Thread() {
...
};
}
无论如何,在这种情况下,AsyncTask
似乎比Thread
更适合。从我在代码中可以看到的内容来看,在这部分中甚至不需要线程化。您实际上并不执行任何需要在单独的Thread
中运行它们的复杂任务,因此任何线程都会适得其反。代码最有可能运行得更好,更顺畅,没有任何线程。