我是java的新手,我想知道如何点击同一页面上的多个按钮而不关闭app力。我试图让代码更简单。现在,第一个按钮工作正常但第二个按钮会在点击时强制关闭应用程序 对不起,如果我不够清楚解释我的问题 我愿意接受任何建议
public static enum CONN_TYPE {
LEJOS_PACKET, LEGO_LCP
}
class UIMessageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE:
_message.setText((String) msg.getData().get(MESSAGE_CONTENT));
break;
case TOAST:
showToast((String) msg.getData().get(MESSAGE_CONTENT));
break;
}
_message.setVisibility(View.VISIBLE);
_message.requestLayout();
}
}
public static final String MESSAGE_CONTENT = "String_message";
public static final int MESSAGE = 1000;
public static final int TOAST = 2000;
private BTSend Right;
private TachoCount tachoCount;
private Toast reusableToast;
private TextView _message;
//static final String START_MESSAGE = "Please make sure you NXT is on and both it and your Android handset have bluetooth enabled";
private static final String GO_AHEAD = "Choose one!";
public static UIMessageHandler mUIMessageHandler;
private final static String TAG = "LeJOSDroid";
public static NXTConnector connect(final CONN_TYPE connection_type) {
Log.d(TAG, " about to add LEJOS listener ");
NXTConnector conn = new NXTConnector();
conn.setDebug(true);
conn.addLogListener(new NXTCommLogListener() {
public void logEvent(String arg0) {
Log.e(TAG + " NXJ log:", arg0);
}
public void logEvent(Throwable arg0) {
Log.e(TAG + " NXJ log:", arg0.getMessage(), arg0);
}
});
switch (connection_type) {
case LEGO_LCP:
conn.connectTo("btspp://NXT", NXTComm.LCP);
break;
case LEJOS_PACKET:
conn.connectTo("btspp://");
break;
}
return conn;
}
public static void displayToastOnUIThread(String message) {
Message message_holder = formMessage(message);
message_holder.what = LeJOSDroid.TOAST;
mUIMessageHandler.sendMessage(message_holder);
}
private static Message formMessage(String message) {
Bundle b = new Bundle();
b.putString(LeJOSDroid.MESSAGE_CONTENT, message);
Message message_holder = new Message();
message_holder.setData(b);
return message_holder;
}
public static void sendMessageToUIThread(String message) {
Message message_holder = formMessage(message);
message_holder.what = LeJOSDroid.MESSAGE;
mUIMessageHandler.sendMessage(message_holder);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUIMessageHandler = new UIMessageHandler();
setContentView(R.layout.main);
_message = (TextView) findViewById(R.id.messageText);
seupNXJCache();
setupTachoCount(this);
setupRight(this);
setupBackward(this);
reusableToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
@Override
protected void onPause() {
super.onPause();
if (Right != null) {
Log.d(TAG, "onPause() closing btSend ");
Right.closeConnection();
Right = null;
}
if (tachoCount != null) {
Log.d(TAG, "onPause() closing btSend ");
tachoCount.closeConnection();
}
}
@Override
protected void onResume() {
super.onResume();
}
public void onCreate1 (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button3 = (Button) findViewById(R.id.button3);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.button2:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run BTSend:" + e.getMessage(), e);
}
break;
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.button3:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run TachoCount:" + e.getMessage(), e);
}
break;
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.button1:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run TachoCount:" + e.getMessage(), e);
}
break;
}
}
});
}
private void seupNXJCache() {
File root = Environment.getExternalStorageDirectory();
try {
String androidCacheFile = "nxj.cache";
File mLeJOS_dir = new File(root + "/leJOS");
if (!mLeJOS_dir.exists()) {
mLeJOS_dir.mkdir();
}
File mCacheFile = new File(root + "/leJOS/", androidCacheFile);
if (root.canWrite() && !mCacheFile.exists()) {
FileWriter gpxwriter = new FileWriter(mCacheFile);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("");
out.flush();
out.close();
_message.setText("nxj.cache (record of connection addresses) written to: " + mCacheFile.getName() + GO_AHEAD);
} else {
_message.setText("nxj.cache file not written as"
+ (!root.canWrite() ? mCacheFile.getName() + " can't be written to sdcard." : " cache already exists.") + GO_AHEAD);
}
} catch (IOException e) {
Log.e(TAG, "Could not write nxj.cache " + e.getMessage(), e);
}
_message.setVisibility(View.VISIBLE);
_message.requestLayout();
}
private void showToast(String textToShow) {
reusableToast.setText(textToShow);
reusableToast.show();
}
}
答案 0 :(得分:0)
您可以在按钮视图中使用this方法,手动点击它。
View.performClick();
您可以对其他按钮执行相同操作,一起单击它们。
很难说,为什么你的应用程序正在关闭,查看你的代码。我注意到的是,你在所有3个按钮clickMethods中做同样的工作。在你正在做的所有3个按钮中:
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
另外,我从你的代码中看到,你只能在一个侦听器上使用一个按钮,将所有你的侦听器代码组合在一起。
View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.button1:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run TachoCount:" + e.getMessage(), e);
}
break;
case R.id.button3:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run TachoCount:" + e.getMessage(), e);
}
break;
case R.id.button2:
try {
tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();
} catch (Exception e) {
Log.e(TAG, "failed to run BTSend:" + e.getMessage(), e);
}
break;
}
}
以后
button2.setOnClickListener(buttonListener);
答案 1 :(得分:0)
检查日志文件是否与内存有关,可以增加堆内存Increase Heap Memory