我做了BroadcastReceiver。调用onReceive方法,并在控制台上显示Log.i,但无论如何都看不到Toast。打包com.example.broadcaster;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("STH happened", "STH HAPPENNED");
Toast.makeText(context, "Intent caught\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG);
}
}我在Log.i之后得到的日志是:
04-17 21:21:16.742: W/Bundle(1685): Key MESSAGE expected String but value was a android.text.SpannableString. The default value <null> was returned.
04-17 21:21:16.742: W/Bundle(1685): Attempt to cast generated internal exception:
04-17 21:21:16.742: W/Bundle(1685): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
04-17 21:21:16.742: W/Bundle(1685): at android.os.Bundle.getString(Bundle.java:1121)
04-17 21:21:16.742: W/Bundle(1685): at com.example.broadcaster.MyReceiver.onReceive(MyReceiver.java:15)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
04-17 21:21:16.742: W/Bundle(1685): at android.os.Handler.dispatchMessage(Handler.java:102)
04-17 21:21:16.742: W/Bundle(1685): at android.os.Looper.loop(Looper.java:136)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 21:21:16.742: W/Bundle(1685): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 21:21:16.742: W/Bundle(1685): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 21:21:16.742: W/Bundle(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 21:21:16.742: W/Bundle(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 21:21:16.742: W/Bundle(1685): at dalvik.system.NativeStart.main(Native Method)
04-17 21:21:16.752: E/SoundPool(383): error loading /system/media/audio/ui/KeypressReturn.ogg
04-17 21:21:16.762: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
04-17 21:21:16.762: E/SoundPool(383): error loading /system/media/audio/ui/KeypressInvalid.ogg
04-17 21:21:16.762: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
04-17 21:21:16.762: W/AudioService(383): onLoadSoundEffects(), Error -1 while loading samples编辑:
广播:
sendBroadcast(new Intent("com.example.MESSAGE_INTENT").putExtra("MESSAGE", ((EditText) findViewById(R.id.textField)).getText()));最后消息为空。
发布于 2014-04-18 09:25:25
确保你在你的logcat中得到了classCastException,展示你的代码。
您需要调用show()来显示toast
Toast.makeText(context, "Intent caught\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG).show();发布于 2014-04-18 12:41:39
您需要在Toast上调用show()。
发布于 2014-04-18 10:18:08
EditText上的getText()返回一个CharSequence,而不是一个字符串。您可以在接收器中调用getText().toString()或使用getExtras().getCharSequence(),具体取决于您是否要保留跨度信息。
https://stackoverflow.com/questions/23146024
复制相似问题