我正在尝试让broadcastReceiver将我的TextView更改为收到短信的邮件正文。当我使用Toast进行警报时,它可以找到,但正如我所说,我希望更改文本视图。应用程序启动但在收到短信时崩溃。请注意,不能直接从扩展broadcastreceiver的类更改textview。所有帮助都表示赞赏:)
这是我的logcat
01-15 02:14:45.714: D/gralloc_goldfish(2551): Emulator without GPU emulation detected.
01-15 02:15:37.165: D/dalvikvm(2551): newInstance failed: no <init>()
01-15 02:15:37.165: D/AndroidRuntime(2551): Shutting down VM
01-15 02:15:37.165: W/dalvikvm(2551): threadid=1: thread exiting with uncaught exception (group=0xb0eca648)
01-15 02:15:37.165: E/AndroidRuntime(2551): FATAL EXCEPTION: main
01-15 02:15:37.165: E/AndroidRuntime(2551): java.lang.RuntimeException: Unable to instantiate receiver com.androidexample.broadcastreceiver.IncomingSms: java.lang.InstantiationException: can't instantiate class com.androidexample.broadcastreceiver.IncomingSms; no empty constructor
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.os.Looper.loop(Looper.java:137)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-15 02:15:37.165: E/AndroidRuntime(2551): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 02:15:37.165: E/AndroidRuntime(2551): at java.lang.reflect.Method.invoke(Method.java:525)
01-15 02:15:37.165: E/AndroidRuntime(2551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-15 02:15:37.165: E/AndroidRuntime(2551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-15 02:15:37.165: E/AndroidRuntime(2551): at dalvik.system.NativeStart.main(Native Method)
01-15 02:15:37.165: E/AndroidRuntime(2551): Caused by: java.lang.InstantiationException: can't instantiate class com.androidexample.broadcastreceiver.IncomingSms; no empty constructor
01-15 02:15:37.165: E/AndroidRuntime(2551): at java.lang.Class.newInstanceImpl(Native Method)
01-15 02:15:37.165: E/AndroidRuntime(2551): at java.lang.Class.newInstance(Class.java:1130)
01-15 02:15:37.165: E/AndroidRuntime(2551): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
这是IncomingSms类
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
private String senderNum;
private String message = "No message recieved from bank";
private TextView textView;
public IncomingSms(TextView textView) {
this.textView = textView;
}
// Get the object of SmsManager
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();
senderNum = phoneNumber;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
this.textView.setText(message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
这是我的BroadcastNewSms类
public class BroadcastNewSms extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidexample_broadcast_newsms);
new IncomingSms((TextView) findViewById(R.id.message));
}
}
答案 0 :(得分:0)
您需要根据堆栈跟踪提供无参数构造函数
public IncomingSms() {}
答案 1 :(得分:0)
这行代码无效
new IncomingSms((TextView) findViewById(R.id.message));
因为当新的短信出现时,Android会创建IncomingSms
类的新实例 - 它不会使用您的实例。
这就是你收到错误的原因 - IncomingSms没有默认构造函数,而且Android无法创建接收器。
您可以添加一个:
public IncomingSms() {
}
但是你没有对TextView的引用。您应该让接收方向任何UI元素广播本地意图(LocalBroadcastManager)以自行更新。