简体   繁体   English

Android:按钮不起作用

[英]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. 因此,此页面上有4个按钮...其中3个按钮可以正常工作,第4个按钮(链接到ToolsTableLayout.class的按钮)根本不响应。 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.. R.id.toolDB是没有响应的按钮。

and here is the ToolsTableLayout.java class (which does not open): 这是ToolsTableLayout.java类(无法打开):

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? 不确定是否需要XML文件? if so let me know and I will update. 如果是这样,请告诉我,我会进行更新。

You did not set onClickListener for 4th button. 您没有为第四个按钮设置onClickListener。 Please add 请加

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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