0

Previously I had error - something about some loop, I've seen info that it is necessary in that case to start a new thread for button, but still nothing happens, thought logs show no errors now.

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final int CAPTURE = 9003;
    Button button;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.capture);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show();
                        Log.d("BUTTON","CLICKED");
                    }
                });
            }
        });
        Intent intent = new Intent(this, CaptureActivity.class);
        intent.putExtra(CaptureActivity.AutoFocus, true);
        startActivityForResult(intent, CAPTURE);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Andy D
  • 125
  • 13
  • you need to write button.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show(); Log.d("BUTTON","CLICKED"); } }); – Dilip Dec 12 '17 at 15:46
  • 1
    it seems from the code that you should be redirected directly to CaptureActivity.class as u have `startActivityForResult`call inside oncreate.. So there is no event firing on button seems possible.. – Aalap Patel Dec 12 '17 at 16:00
  • @Dilip thanks for attention, but can't understand where exactly put it, would you mind to create an answer ? – Andy D Dec 12 '17 at 16:01
  • you don't need this , no need to tell the OS to execute code on UI where it actually will be executed on UI and as @Cristian said you need to initialize context variable as well or use `MainActivity.this` – Pavneet_Singh Dec 12 '17 at 16:02
  • @Aalap Patel it seems you right, I put onClick into another class now at least I have flickering screen on button click. – Andy D Dec 12 '17 at 16:06

1 Answers1

1

runOnUiThread mean you are telling the UI thread to execute the passed Runnable as instruction so it won't create a new thread.

Solution : I assume you this code is for demo purpose for threading and UI updation so one of best alternative is to AsyncTask

and remove runOnUiThread function , no need of it


You need to remove this code or move it inside run method

Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);

Because you are currently in CaptureActivity not on MainActivity. OnCreate will directly take you to the CaptureActivity where you are expecting the code of MainActivity to run (probably they have same UI)

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    I don't think he needs an AsyncTask to handle a button click event though – PKlumpp Dec 12 '17 at 15:48
  • @ZerO yeah but the suggested solution is to have threading via doInBackGround and execute UI work via `postexecute` which will work as a thread as well as UI updater – Pavneet_Singh Dec 12 '17 at 15:49
  • It seesm onClick() should have been in CaptureActivity class. – Andy D Dec 12 '17 at 16:07
  • @AndyD that's your wish though as per the current code , i suggested as per the behavior your question asked for and still no need of `runOnUiThread` :) – Pavneet_Singh Dec 12 '17 at 16:11
  • @AndyD and why you would like to have `MainActivity` just to go to next activity without doing nothing, no need of it – Pavneet_Singh Dec 12 '17 at 16:18
  • @Pavneet_Singh you right, it's just left from previous version of my code. – Andy D Dec 12 '17 at 16:19