我的应用程序有一个日志活动,用户输入手机号码。他可以使用按钮(“ENVIAR”)向该号码发送代码“1234020”,并使用内容和传入消息的号码更改TextView。
使用“SIGUIENTE”按钮,应用程序必须转到第二个活动LucesAlarma
。在那里,他们用第一个活动的电话号码修改TextView
并发送(使用“ENVIARLA”按钮)并接收消息。
“Atras”按钮应返回第一个活动。问题是在MainActivity
中按下“Siguiente”按钮后应用程序崩溃了。我相信Manifest存在问题。
主要活动:
public class MainActivity extends AppCompatActivity {
Button ENVIAR,SIGUIENTE;
EditText NUMEROT;
TextView CONTENIDO;
IntentFilter intentFilter;
private BroadcastReceiver intentReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String MENSAJE = intent.getExtras().getString("mensaje");
String NUMERODELMENSAJE = intent.getExtras().getString("numero");
CONTENIDO=(TextView)findViewById(R.id.tvSMScontenido);
CONTENIDO.setText(MENSAJE+NUMERODELMENSAJE);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
ENVIAR = (Button) findViewById(R.id.bEnviarMA);
SIGUIENTE = (Button)findViewById(R.id.bSiguienteMA);
NUMEROT = (EditText)findViewById(R.id.etNumeroTelf);
ENVIAR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ELMENSAJE = "1234020";
String ELNUMERO = NUMEROT.getText().toString();
EnviarSMS(ELMENSAJE, ELNUMERO);
Toast.makeText(getApplicationContext(), "Ya esta ya", Toast.LENGTH_LONG).show();
}
});
SIGUIENTE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intento = new Intent(MainActivity.this, LucesAlarma.class);
intento.putExtra("numero", NUMEROT.getText().toString());
startActivity(intento);
}
});
}
@Override
protected void onResume() {
registerReceiver(intentReciever, intentFilter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(intentReciever);
super.onPause();
}
protected void EnviarSMS(String elmensaje, String elnumero) {
SmsManager SMS = SmsManager.getDefault();
SMS.sendTextMessage(elnumero, null, elmensaje, null, null);
}
}
第二项活动(LucesAlarma)
public class LucesAlarma extends AppCompatActivity {
TextView NUMEROLA,MENSAJELA;
Button ENVIARLA,ATRAS;
IntentFilter intentFilterLA;
private BroadcastReceiver intentRecieverLA = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
String MENSAJE = intent.getExtras().getString("mensaje");
String NUMERODELMENSAJE = intent.getExtras().getString("numero");
MENSAJELA=(TextView)findViewById(R.id.tvMensajeLA);
MENSAJELA.setText(MENSAJE+NUMERODELMENSAJE);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_luces_alarma);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
final String numeropos =intent.getStringExtra("numero");
NUMEROLA=(TextView)findViewById(R.id.tvNumeroLA);
NUMEROLA.setText(numeropos);
ENVIARLA=(Button)findViewById(R.id.bEnviarLA);
ENVIARLA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ELMENSAJE = "1234020";
String ELNUMERO = numeropos;
EnviarSMS(ELMENSAJE, ELNUMERO);
Toast.makeText(getApplicationContext(), "Enviado", Toast.LENGTH_LONG).show();
}
});
ATRAS=(Button)findViewById(R.id.bAtras);
ATRAS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentoLA = new Intent(LucesAlarma.this, MainActivity.class);
startActivity(intentoLA);
}
});
}
@Override
protected void onResume() {
registerReceiver(intentRecieverLA,intentFilterLA);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(intentRecieverLA);
super.onPause();
}
protected void EnviarSMS(String elmensaje, String elnumero) {
SmsManager SMS = SmsManager.getDefault();
SMS.sendTextMessage(elnumero, null, elmensaje, null, null);
}
}
SMS Reciver java类:
public class ReceptorSMS extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[]messages=null;
String str = "";
String num = "";
String men = "";
if(bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
assert pdus != null;
messages = new SmsMessage[pdus.length];
for (int i=0 ; i<messages.length;i++) {
messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
num = messages[i].getDisplayOriginatingAddress();
str += "Mensaje de" +messages[i].getOriginatingAddress();
str += ":";
str += messages[i].getMessageBody();
str += "\n";
men = messages[i].getMessageBody();
}
// Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
broadcastIntent.putExtra("mensaje", men);
broadcastIntent.putExtra("numero", num);
context.sendBroadcast(broadcastIntent);
}
}
}
而清单,我无法确定问题:'v
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.josue.smartwasip">
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LucesAlarma"
android:label="@string/title_activity_luces_alarma"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.josue.smartwasi" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name=".ReceptorSMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
这是注释:
03-08 15:39:15.059 1385-1385/com.example.josue.smartwasip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.josue.smartwasip, PID: 1385
java.lang.RuntimeException: Unable to resume activity {com.example.josue.smartwasip/com.example.josue.smartwasip.LucesAlarma}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.IntentFilter.writeToParcel(android.os.Parcel, int)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.IntentFilter.writeToParcel(android.os.Parcel, int)' on a null object reference
at android.app.ActivityManagerProxy.registerReceiver(ActivityManagerNative.java:3008)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1184)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1152)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1146)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
at com.example.josue.smartwasip.LucesAlarma.onResume(LucesAlarma.java:76)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6312)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
您只是在[14, 56, 32, 40, 10]
声明IntentFilter intentFilterLA;
。您还需要像LucesAlarma
中为MainActivity.onCreate()
分配一个值。
将这两行添加到IntentFilter
LucesAlarma.onCreate()