3

Last time I was working on same project and I was able to use Toasts. But today when I try to use Toast nothing is showing. I tried my best to solve this problem but not succeeded.

This is what I am executing [UPDATED].

package com.azeem.abubakar.dbm;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Button btn_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialization
        btn_start = (Button) findViewById(R.id.btn_start);

        //Click Listener for Start Button
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View tView) {

                Log.d("[INFO]", "Just Above Toast!");
                Toast.makeText(MainActivity.this, "Start button Clicked", Toast.LENGTH_SHORT).show();
                Log.d("[INFO]", "Just Below Toast!");

            }
        });

    }
}

Here is what Logcat displays when I click Start Button.

01-13 19:28:36.473 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@ede0223[MainActivity]: ViewPostImeInputStage processPointer 0
01-13 19:28:36.474 26670-26670/com.azeem.abubakar.dbm W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
01-13 19:28:36.476 26670-26670/com.azeem.abubakar.dbm E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
01-13 19:28:36.538 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@ede0223[MainActivity]: ViewPostImeInputStage processPointer 1
01-13 19:28:36.543 26670-26670/com.azeem.abubakar.dbm D/[INFO]: Just Above Toast!
01-13 19:28:36.552 26670-26670/com.azeem.abubakar.dbm D/TextView: setTypeface with style : 0
01-13 19:28:36.560 26670-26670/com.azeem.abubakar.dbm D/[INFO]: Just Below Toast!
01-13 19:28:36.565 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: ThreadedRenderer.create() translucent=true
01-13 19:28:36.571 26670-26670/com.azeem.abubakar.dbm D/InputTransport: Input channel constructed: fd=82
01-13 19:28:36.571 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: setView = android.widget.LinearLayout{5b8d649 V.E...... ......I. 0,0-0,0} touchMode=true
01-13 19:28:36.572 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: dispatchAttachedToWindow
01-13 19:28:36.593 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[277,1596][802,1728] result=0x27 surface={isValid=true -750856192} surfaceGenerationChanged=true
    mHardwareRenderer.initialize() mSurface={isValid=true -750856192} hwInitialized=true
01-13 19:28:36.594 26670-26713/com.azeem.abubakar.dbm D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [525x132]-format:1
01-13 19:28:36.631 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: MSG_RESIZED_REPORT: frame=Rect(277, 1596 - 802, 1728) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
01-13 19:28:38.565 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: mHardwareRenderer.destroy()#4
01-13 19:28:38.582 26670-26670/com.azeem.abubakar.dbm D/ViewRootImpl@3da3450[Toast]: dispatchDetachedFromWindow
01-13 19:28:38.591 26670-26670/com.azeem.abubakar.dbm D/InputTransport: Input channel destroyed: fd=82

Please help me. Thanks.

UPDATE:

Notification is enabled for this app in settings.

But Toast is not showing. What I figured out, it may be device related problem please correct me if I'm wrong. The reason I said it is a device related problem is that last time I used Toasts every thing was working but after I installed a security update in my Samsung Galaxy device toast not showing at all.

My Mobile Model is Samsung A5(2016). Please look at the attached screenshots for software information. Software Information(1/2) Software Information(2/2)

Guy Luz
  • 3,372
  • 20
  • 43
Abubakar Azeem
  • 314
  • 1
  • 4
  • 14

5 Answers5

2

1- Please verify are you trying to show Toast on Main Thread?? if not try this code

((MainActivity)context).runOnUiThread(new Runnable() {
    public void run() {
           Toast.makeText(MainActivity.this, "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();

    }
});

2- Secondly try using YourActivith.this instead of getBaseContext()

3- Sometimes when keyboard is open toast is visible but behind your soft keyboard try close keyboard first and then see.

Hope any of one will help you!

Intsab Haider
  • 3,491
  • 3
  • 23
  • 32
1

Try changing this:

getBaseContext()

to getApplicationContext() or getApplication() or this or MainActivity.this (android java page.this).

Exaple:

Toast.makeText(getApplicationContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();

Be sure to user This on onTextChanged method:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText edtxt_fileName = findViewById(R.id.edtxt_fileName);
    edtxt_fileName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String fileName = s.toString();
            if (fileName.equals("")) {
                Log.d("[INFO]", "Just Above Toast!");
                Toast.makeText(getApplicationContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
                Log.d("[INFO]", "Just Below Toast!");
            }
            else{   // to show every text change 
                Toast.makeText(getApplicationContext(), "Text: " + fileName, Toast.LENGTH_SHORT).show();   
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Guy Luz
  • 3,372
  • 20
  • 43
  • Thanks for your answer. I replaced getBaseContext() but Toast still not showing. – Abubakar Azeem Jan 13 '19 at 13:02
  • Updated my answer, @AbubakarAzeem please check my answer again and if this or any answer has solved your question consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Guy Luz Jan 13 '19 at 13:50
  • Yes i understand @guy that i should mark the correct answer. But my probem is not solved and i have tried all suggestions. Please check my post, i have updated it. There is no problem in code and Toast is working on other devices. Thanks for your concern. – Abubakar Azeem Jan 13 '19 at 14:41
  • I checked the code on my device and it is working. Could be the update, sorry bro. – Guy Luz Jan 13 '19 at 15:10
1

Try change this :

 Toast.makeText(getBaseContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();

Into :

 Toast.makeText(getApplicationContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

Try getApplicationContext() instead of getBaseContext(). Go to this link to find where to use which context.
difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

0

try to initialize context in oncreate and pass that context while displaying code this may work

Muhammad Haroon
  • 210
  • 2
  • 11