更改事件的布局

时间:2014-03-30 14:16:38

标签: android

我在更改事件布局时遇到问题。我有3个布局和broadcastReceiver与SMS接收器。当我收到一个布局数量的消息时,我尝试将activity_main1.xml更改为activity_main2.xml。

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Vibrator;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.view.LayoutInflater;
import android.webkit.WebView.FindListener;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.net.Uri;

public class BroadReceiver extends BroadcastReceiver {
    MainActivity main;
    private static final String Action = "android.provider.Telephony.SMS_RECEIVED";



    @Override
    public void onReceive(Context context, Intent intent) {

        StringBuilder sb = new StringBuilder();
        String phoneNumber = "";
        String smsMessage= "";


        if(intent.getAction() == Action){
            Bundle bundle = intent.getExtras();

            if(bundle != null){
                // pobieranie smsow
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                for (SmsMessage message : messages) {
                    sb.append("SMS From: ");
                    phoneNumber = message.getDisplayOriginatingAddress();
                    sb.append(phoneNumber);
                    sb.append("\nMessage: ");
                    smsMessage = message.getDisplayMessageBody();
                    sb.append(smsMessage);
                }

                Log.d("asd", "onReceive - message: " + sb);


                Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG)
                        .show();


                if (phoneNumber.equals("+4800000000")) {
                    Log.d("asd", "correct phone");
                    if(smsMessage.equals("1")){
                        LayoutInflater inflater = main.getLayoutInflater(); // HERE I TRIED TO USE setContentView(R.layout...) but doesn't work.
                        inflater.inflate(R.layout.activity_main1,null);
                    }else if(smsMessage.equals("2")){
                        LayoutInflater inflater = main.getLayoutInflater();
                        inflater.inflate(R.layout.activity_main2,null);
                    }else if(smsMessage.equals("3")){
                        LayoutInflater inflater = main.getLayoutInflater();
                        inflater.inflate(R.layout.activity_main3,null);

                        }



                    }
                }
            }
        }

    }

这是日志;

03-30 16:09:28.193: E/AndroidRuntime(15095): FATAL EXCEPTION: main
03-30 16:09:28.193: E/AndroidRuntime(15095): java.lang.RuntimeException: Unable to start receiver pl.pawelfrydrych.broadsms.BroadReceiver: java.lang.NullPointerException
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2541)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.app.ActivityThread.access$1600(ActivityThread.java:159)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1392)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.os.Looper.loop(Looper.java:137)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.app.ActivityThread.main(ActivityThread.java:5419)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at java.lang.reflect.Method.invoke(Method.java:525)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at dalvik.system.NativeStart.main(Native Method)
03-30 16:09:28.193: E/AndroidRuntime(15095): Caused by: java.lang.NullPointerException
03-30 16:09:28.193: E/AndroidRuntime(15095):    at pl.pawelfrydrych.broadsms.BroadReceiver.onReceive(BroadReceiver.java:67)
03-30 16:09:28.193: E/AndroidRuntime(15095):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2525)
03-30 16:09:28.193: E/AndroidRuntime(15095):    ... 10 more

0 个答案:

没有答案