按钮不再执行它应该执行的操作

时间:2014-05-15 05:00:48

标签: android button onclicklistener

我有一个按钮,用于从一个活动切换到另一个用于工作的按钮,但自从我添加了另一个按钮的编码以调用zxing的扫描功能后,该按钮不再执行任何操作。 对于那些愿意帮助的人来说,这是我的MainActivity.java

 package com.example.mdpmk1;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.content.Context;
    import android.net.wifi.ScanResult;
    import android.widget.Button;
    import com.google.zxing.integration.android.IntentIntegrator;
    import com.google.zxing.integration.android.IntentResult;

    public class MainActivity extends Activity {

    Button button;

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

    public void addListenerOnButton() {

        final Context context = this;


        button = (Button) findViewById(R.id.scan);


        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(v.getId()==R.id.scan){
                //scan

                    IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                    integrator.initiateScan();
                }


            }

        });

    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
        //super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {

            } else if (resultCode == RESULT_CANCELED) {

            }
        }
    }





    Button button2;

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public void addListenerOnButton1() {

        final Context context = this;


        button2 = (Button) findViewById(R.id.getResults);


        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, ScanResult.class);
                startActivity(intent);



            }

        });

    }


    }

没有响应的按钮是button2,所以如果你可以帮助我做任何可爱的事情,提前谢谢。感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

对于这两个按钮您正在添加相同的侦听器

  

addListenerOnButton();

     

addListenerOnButton1();

请相应更改按钮2

答案 1 :(得分:0)

    public class MainActivity extends Activity {
Button bt, bt2;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                bt = (Button) findViewById(R.id.scan);
                bt2 = (Button) findViewById(R.id.getResults);



                bt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         // do initiatescan
                    }
                });

                bt2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent intent = new Intent(MainActivity.this, ScanResult.class);
                        startActivity(intent);



                    }

                });



            }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
                {
                    //super.onActivityResult(requestCode, resultCode, intent);
                    if (requestCode == 0) {
                        if (resultCode == RESULT_OK) {

                        } else if (resultCode == RESULT_CANCELED) {

                        }
                    }
                }


            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

        }

修改:使用此方法更改onCreate方法并进行检查。希望它有效。