0

I have button at the top of sreeen and camera preview at beneath, when I press button screen is blinking, but want it to show Toast. It stopped to blink after I commented out Toast.makeText(), and log shows that button was clicked.

Component tree (just in case, I don't want to change it):

enter image description here

The code:

public final class CaptureActivity extends AppCompatActivity {
 Button button;
 Context context;

...

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.capture);

        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                        Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show();
                        Log.d("BUTTON","CLICKED");
                    }
        });

        mPreview = (CameraSourcePreview) findViewById(R.id.preview);
        mGraphicOverlay = (GraphicOverlay<Graphic>) findViewById(R.id.graphicOverlay);
Andy D
  • 125
  • 13

2 Answers2

0

Where have you defined Context?

It's null. It may be ok if you put getApplicationContext()

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
0

I think it's better to show toast like below in activities:

Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show(); I mean passing this to first argument of toast.

salmanseifian
  • 1,082
  • 3
  • 9
  • 26