该方法的onClick(视图)从类型新的Thread(){}从不在本地使用 [英] The method onClick(View) from the type new Thread(){} is never used locally

查看:307
本文介绍了该方法的onClick(视图)从类型新的Thread(){}从不在本地使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图多线程我的code,但是当我把 OnClickListener 在一个新的,这个错误在Eclipse中出现:

I'm attempting to multithread my code, but when I put the OnClickListener in a new Thread, this error comes up in Eclipse:

The method onClick(View) from the type new Thread(){} is never used locally

我的code 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) {

    }
}

线程t =新主题()。有谁人知道为什么发生这种情况,以及如何解决它?

The Thread is at Thread t = new Thread(). Does anyone one know why this happens, and how to fix it?

推荐答案

在定义点击监听器通过它查找听者的XML在活动视图,没有任何随机线程您可能已经创建。你的架构需要改变,这样onClick的是在活动,然后该方法使用线程,而不是

When you define the click listener for a view via the XML it looks for the listener in your Activity, not any random threads you may have created. Your architecture needs changing so that the onClick is in the Activity, and then that method uses the thread instead

这篇关于该方法的onClick(视图)从类型新的Thread(){}从不在本地使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆