问题
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);
}
});
}
}
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.
回答1:
For both buttons You are adding Same Listener
addListenerOnButton();
addListenerOnButton1();
please change in button2 accordingly
回答2:
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;
}
}
Edit:Change your onCreate method with this one and check. Hope it works.
来源:https://stackoverflow.com/questions/23669615/button-no-longer-does-what-it-is-supposed-to-do