0

I'm new at java and i would like to know how to click multiple buttons on same page without making the app force close. I tried to make the code more simple. For now , the first button is work well but the second button will force close the app when get clicked.
Im sorry if im not clear enough to explain my problem
im open for any advices

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();
    }

}
VMAtm
  • 27,943
  • 17
  • 79
  • 125

2 Answers2

0

You can use this method to your Button View, to manually click it.

View.performClick();

The same you can do to other button, to click them together.

It is hard to say, why your app is closing, looking on your code. What I have noticed, is that you are doing the same work, in all 3 buttons clickMethods. In all 3 buttons you are doing:

tachoCount = new TachoCount();
_message.setVisibility(View.INVISIBLE);
tachoCount.start();

Also, I see from your code, that you can use only one button onCLick listener, by combine all your listeners code together.

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

   }

And later

button2.setOnClickListener(buttonListener);
VadymVL
  • 5,366
  • 3
  • 26
  • 41
  • thank you @VadymVL , but im not sure where i should put that. Is that before button2.setOnClickListener(buttonListener); ? – nabila gina Mar 26 '15 at 11:25
  • Yes, it goes before section, when you set to buttons their Listeners. `Button button3 = (Button) findViewById(R.id.button3); Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2); button3.setOnClickListener(buttonListener); button2.setOnClickListener(buttonListener); button1.setOnClickListener(buttonListener);` – VadymVL Mar 26 '15 at 11:26
  • @nabila gina, also, I have noticed, that your `onCreate` method is has duplicate with name `onCreate1`. Maybe is it the reason of crashing? You should do all logic in only one, or must remove from duplicate calls to super. – VadymVL Mar 26 '15 at 11:28
  • when i put the code, i dont get any action after i click buttons. the logcat said readLCP error – nabila gina Mar 26 '15 at 11:51
  • @nabila gina, I don't know what this error means. But check your `onCreate1` method, where is it has called? Are you using it? Because your button logic now are in it, but should be in `onCreate` method, – VadymVL Mar 26 '15 at 11:55
  • yeah i also noticed onCreate has a duplicate. i havent find any solution for that – nabila gina Mar 26 '15 at 11:59
  • So move your code from `onCreate1` to `onCreate` and remove `onCreate1`. – VadymVL Mar 26 '15 at 12:00
  • i found it. I used onCreate on Override for message handler – nabila gina Mar 26 '15 at 12:09
  • But are you using onCreate**1**? – VadymVL Mar 26 '15 at 12:11
  • I put it on onCreate , the code run, but when i try the second button it force close again. From logcat, it said that it failed connect to nxt (the button is for move the motor on nxt) . i think now my problem is to maintain connection to the nxt so then when i push another button it will giving the action.... thank you – nabila gina Mar 26 '15 at 12:17
0

Check your logfile if it is memory related issue you can increase you heap memory Increase Heap Memory

Community
  • 1
  • 1
user3374790
  • 227
  • 3
  • 13