按钮不再做它应该做的事情 [英] Button no longer does what it is supposed to do

查看:72
本文介绍了按钮不再做它应该做的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,用于从一个活动切换到另一个可以工作的活动,但自从我添加了另一个按钮的编码以调用 zxing 的扫描功能后,该按钮就不再做任何事情了.这是我的 MainActivity.java 给那些愿意提供帮助的人

I have a button that is meant to swap from one activity to another which use to work but ever since i added the coding for another button to call for zxing's scanning feature the button no longer does anything at all. This is my MainActivity.java for those who are willing to help

 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,所以如果你能帮我做任何可爱的事情,提前致谢.感谢所有帮助.

The button that isn't responding is button2 so if you can help me with anything that would be lovely, Thanks in advance. All help is appreciated.

推荐答案

    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 方法并检查.希望它有效.

Edit:Change your onCreate method with this one and check. Hope it works.

这篇关于按钮不再做它应该做的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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