我想将我通过短信收到的按摩文本设置到我主要活动的文本视图中。我努力了很久,我在互联网上到处搜索,但它不起作用。请帮我
如何在其他活动的 TextView 中设置文本?我试过这样做,但它不起作用
我认为方法“TextView textViewAlarmMassage = (TextView) findViewById(R.id.textViewAlarmMassage);” 在 mainActivity 中不起作用,因为 programkm 停在这一行
主要活动
public class MainActivity extends Activity {
//这是另一个代码
public void showAlarmMassage(){
//Connect to SMSReceiver Class
*SMSReceiver receiver = new SMSReceiver();*
//Get the Alarm Massage
*String massage = receiver.getAlarmMassage().toString();*
//Initialize the TextView
*TextView textViewAlarmMassage = (TextView) findViewById(R.id.textViewAlarmMassage);*
//Set the Alarm Massage in the TextView
*textViewAlarmMassage.setText(massage);*
}
}
公共类 SMSReceiver 扩展 BroadcastReceiver{
public String alarmMassage;
public String senderAddress;
@Override
public void onReceive(Context cxt, Intent intent){
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
alarmMassage = smsMessage[0].getDisplayMessageBody();
senderAddress = smsMessage[0].getDisplayOriginatingAddress();
//Set the AlarmMassage in the Setter Constructor
*setAlarmMassage(alarmMassage);*
*MainActivity mainActivity = new MainActivity();*
//Here I want to start the showAlarmMassage methode in the MainActivity
*mainActivity.showAlarmMassage();*
String toast = "Received SMS from: " + senderAddress;
toast += "\nMessage: " + alarmMassage;
Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();
}
}
//Getting AlarmMassage
*public String getAlarmMassage(){*
return this.alarmMassage;
}
//setting AlarmMassage
*public void setAlarmMassage(String alarmMassage){*
this.alarmMassage = alarmMassage;
}
}
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.ffnt.datenbank"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_startseite" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EntryActivity"
android:label="@string/title_activity_entry" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ViewAllEntriesActivity"
android:label="@string/title_activity_view_all_entries" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AudioRecording"
android:label="@string/title_audio_recording" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StatusReceiver">...</receiver>
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
日志
01-03 17:04:30.598: E/AndroidRuntime(22422): 致命异常: main 01-03 17:04:30.598: E/AndroidRuntime(22422): java.lang.RuntimeException: 无法在.ffnt 启动接收器。 datenbank.SMSReceiver: java.lang.NullPointerException 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2287) 01-03 17:04:30.598: E /AndroidRuntime(22422): 在 android.app.ActivityThread.access$1600(ActivityThread.java:140) 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 android.app.ActivityThread$H.handleMessage(ActivityThread .java:1313) 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-03 17:04:30.598: E/AndroidRuntime(22422 ): 在 android.os.Looper.loop(Looper.java:137) 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 android.app.ActivityThread。main(ActivityThread.java:4921) 01-03 17:04:30.598: E/AndroidRuntime(22422): at java.lang.reflect.Method.invokeNative(Native Method) 01-03 17:04:30.598: E/AndroidRuntime (22422): 在 java.lang.reflect.Method.invoke(Method.java:511) 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:1038)01-03 17:04:30.598:E/AndroidRuntime(22422):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)01-03 17:04: 30.598: E/AndroidRuntime(22422): at dalvik.system.NativeStart.main(Native Method) 01-03 17:04:30.598: E/AndroidRuntime(22422): 由: java.lang.NullPointerException 01-03 17: 04:30.598: E/AndroidRuntime(22422): at.ffnt.datenbank.MainActivity.showAlarmMassage(MainActivity.java:248) 01-03 17:04:30.598: E/AndroidRuntime(22422): at.ffnt.datenbank .SMSReceiver.onReceive(SMSReceiver.java:37) 01-03 17:04:30.598: E/AndroidRuntime(22422): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2280) 01-03 17:04:30.598: E/AndroidRuntime(22422): ... 10 更多