我开发了一个有15个屏幕的应用程序.现在我想在所有这15个屏幕中显示自定义Toast消息.为此,我夸大了一个布局.但它只在一个屏幕上工作.所以,我写了一个方法来Toast在所有屏幕上显示自定义.每当我想显示toast消息时,我就会调用该方法.但我得到了java.lang.NullPointerException.怎么解决这个?以下是我的代码,
public static void showToastMessage(String message){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) ((Activity) context).findViewById(R.id.customToast));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(message);
// Toast...
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
Run Code Online (Sandbox Code Playgroud)
记录是
java.lang.NullPointerException
at com.guayama.utilities.CommonMethods.showToastMessage(CommonMethods.java:474)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
Run Code Online (Sandbox Code Playgroud) 我在我的android应用程序中使用过多汁.只要我愿意,我可以展示吐司而不是
Toast.LENGTH_LONG
Run Code Online (Sandbox Code Playgroud)
和
Toast.LENGTH_SHORT
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我一些有用的代码段.谢谢.
在按照以下条件单击"boton_continuar"按钮时,我正在尝试显示吐司:EditExt vacuum(numero_celular)和ckeckbox(check)选中no.这是我的代码
boton_continuar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Verificamos que se halla clikeado y se halla ingresado
if(check.isChecked() && numero_celular.getText().length() != 0){
Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
startActivity(intent);
}else{
Context contexto = getApplicationContext();
if(!check.isChecked()){
Toast.makeText(contexto, "Debe aceptar los términos", 2000);
}
if(numero_celular.getText().length() == 0){
Toast.makeText(contexto, "Debe ingresar su número", 2000);
}
}
}});
Run Code Online (Sandbox Code Playgroud) 我想在主屏幕和任何应用程序上显示通知,如消息应用程序Line,它会显示一个通知框,消息在其中显示一秒钟然后消失.
这样的事情:

那么有什么方法可以做到这一点?这是一个定制的吐司吗?或自定义对话框?
我是一个新手android学习者.我正在尝试创建吐司,当用户选择AutoCompleteTextView中的项目时.我正在读取strings.xml中的值,这是正常工作,但我很困惑我如何(甚至处理程序)我需要创建吐司.下面是我的代码1- activity.xml
<AutoCompleteTextView android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
/>
strings.xml
<string-array name="termsarray">
<item>emulator</item>
<item>ant</item>
<item>string.xml</item>
<item>activity</item>
<item>workspace</item>
<item>adt</item>
<item>manifest.xml</item>
<item>package</item>
<item>layout</item>
<item>toast</item>
<item>adb</item>
</string-array>
//activity.java
public class TermActivity extends Activity {
String[] categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categories=getResources().getStringArray(R.array.termsarray);
setContentView(R.layout.activity_term);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, categories);
AutoCompleteTextView textView =
(AutoCompleteTextView) findViewById(R.id.myautocomplete);
textView.setThreshold(3);
textView.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud) 我正在练习服务,但坚持开始.我希望每隔3秒从服务中出现一次祝酒词.
我在做什么
myservice.java
package com.example.servicedemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class myService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public myService() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service created", 3000).show();
} …Run Code Online (Sandbox Code Playgroud) 我有简单的自定义吐司,它在类范围活动中工作正常,但在片段中我有一个问题.findviewbyid方法不能在碎片中工作!!
我该怎么办呢?
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.customtoast, (ViewGroup)findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText(message);
layouttoast.findViewById(R.id.imagetoast)).setBackgroundResource(R.drawable.icon);
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
Run Code Online (Sandbox Code Playgroud) 在 ImageButton 中创建 Toast 时出现错误
无法解析方法'makeText(匿名android.view.View.OnclickListener,java.lang.String,int)'