上次我从事同一项目,并且能够使用Toasts。但是今天,当我尝试使用Toast时,什么也没显示。我尽力解决了这个问题,但没有成功。
这就是我正在执行的[更新]。
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!");
}
});
}
}
这是我单击“开始”按钮时Logcat显示的内容。
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
请帮助我。谢谢。
更新:
在设置中为此应用启用了通知。
但是Toast没有显示。我发现,可能是与设备有关的问题,如果我错了,请纠正我。我之所以说这是与设备相关的问题,是因为我上次使用Toasts时一切正常,但是在我的Samsung Galaxy设备Toast中安装了安全更新后,却完全没有显示。
答案 0 :(得分:2)
1-请验证您是否要在主线程上显示Toast?如果不尝试,请输入此代码
((MainActivity)context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
}
});
2-其次,尝试使用YourActivith.this代替getBaseContext()
3-有时可以看到键盘打开时,但在软键盘后面尝试先关闭键盘,然后再看。
希望任何一个人都能为您提供帮助!
答案 1 :(得分:1)
尝试更改此内容:
getBaseContext()
到getApplicationContext()
或getApplication()
或this
或MainActivity.this
(android java page.this)。
例如:
Toast.makeText(getApplicationContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
请确保在onTextChanged
方法上使用“ This”:
@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) {
}
});
答案 2 :(得分:1)
尝试更改此内容:
Toast.makeText(getBaseContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
进入:
Toast.makeText(getApplicationContext(), "Enter Valid File Name!!", Toast.LENGTH_SHORT).show();
答案 3 :(得分:0)
尝试使用newMessages
而不是getApplicationContext()
。转到此链接以查找在哪里使用哪个上下文。
difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this
答案 4 :(得分:0)
尝试在oncreate中初始化上下文并在显示可能有效的代码时传递该上下文