简体   繁体   中英

Android: button does not work

So there is 4 buttons on this page... 3 of them work fine, the 4th button, which links to ToolsTableLayout.class does not respond at all. There are no erros, the app does not crash or anything... you just click the button and nothing happens.

Code for the button class:

public class MainMenu extends Activity implements OnClickListener{

private String result;
boolean isScanout;
public static final String SCAN_RESULT = "MyPreferencesFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    Button ScanOut=(Button)findViewById(R.id.scanout);
    ScanOut.setOnClickListener(this);

    Button ScanIn=(Button)findViewById(R.id.scanin);
    ScanIn.setOnClickListener(this);

    Button EndSession = (Button) findViewById(R.id.endsession);
    EndSession.setOnClickListener(this);
}


        @Override
        public void onClick(View v) {

            if(v.getId()==R.id.scanout){

                isScanout = true;
                IntentIntegrator.initiateScan(this);
            }

            else if(v.getId()==R.id.scanin){

                isScanout = false;
                IntentIntegrator.initiateScan(this);

                }
            else if(v.getId()==R.id.endsession){
                Intent endsessionintent = new Intent(MainMenu.this, MainActivity.class);
                startActivity(endsessionintent);
            }
            else if(v.getId()==R.id.toolDB){
                Intent i = new Intent(MainMenu.this, ToolsTableLayout.class);
                startActivity(i);
            }

        }


        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch(requestCode) {
            case IntentIntegrator.REQUEST_CODE: {
                if (resultCode != RESULT_CANCELED) {
                    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

                            if (scanResult != null) {
                            String qrCode = scanResult.getContents();

                            SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
                            SharedPreferences.Editor editor = codeHack.edit();
                            editor.putString("entry", qrCode);
                            editor.commit();
                            }
                }
            }
            }
        }


@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;
}
}

R.id.toolDB is the button that makes no response..

and here is the ToolsTableLayout.java class (which does not open):

public class ToolsTableLayout extends Activity {

public static final String SCAN_RESULT = "MyPreferencesFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablelayout2);

    SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
    String QRcode = codeHack.getString("entry", "unregistered");

    StringTokenizer token = new StringTokenizer(QRcode," ");
    String name = token.nextToken();
    String quantity = token.nextToken();

    TextView t1 = (TextView) findViewById(R.id.slot1b);
    TextView t2 = (TextView) findViewById(R.id.slot2b);
    TextView t3 = (TextView) findViewById(R.id.slot3b);

    ToolDB info = new ToolDB(this);
    info.open();
    String c1 = info.getRowID();
    info.createEntry(name, quantity);
    info.close();

    t1.setGravity(Gravity.CENTER_HORIZONTAL);
    t2.setGravity(Gravity.CENTER_HORIZONTAL);
    t3.setGravity(Gravity.CENTER_HORIZONTAL);

    t1.setText(c1);
    t2.setText(name);
    t3.setText(quantity);
}

}

not sure if the XML files are needed? if so let me know and I will update.

You did not set onClickListener for 4th button. Please add

Button btnToolDb = (Button) findViewById(R.id.toolDB);
btnToolDb.setOnClickListener(this);

从您提供的代码来看,我相信您忘记了创建ClickListener并将其ClickListener到代码中缺少的第四个按钮上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM