0

我有以下代码:

public class SmsReceiver extends BroadcastReceiver
{
    private DBOperations DataDBoperation;
    String str = ""; 
    String password;
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                             
                str += msgs[i].getMessageBody().toString();
            }
            //---if incomming msg is equal to Msg pattern
            int triggerCommand = getMsgPattern(context);
            if(triggerCommand == 1 || triggerCommand == 2 || triggerCommand == 3 || triggerCommand == 4 || triggerCommand == 5 || triggerCommand == 6)
            {
                switch(triggerCommand)
                {
                case 1:
                {   ///// BACKUP
                    Intent i=new Intent(context,UploadContent.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    context.startActivity(i);
                    break;
                }
                case 2:
                {
                    //// FORMAT
                    Intent j=new Intent(context,DeleteContent.class);
                    j.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    context.startActivity(j);
                    break;
                }
                }
                //-------- delete this sms here ----------
                abortBroadcast();
            }
        } 

public int getMsgPattern(Context c)
    {
        DataDBoperation = new DBOperations(c);
        String[] valuesPassword = DataDBoperation.getUserPasswordsString();
        password = valuesPassword[0];
        String pattern1 = "BACKUP *" + password + "*";
        String pattern2 = "FORMAT *" + password + "*";
        String pattern3 = "LOCATE *" + password + "*";
        String pattern4 = "WIPE *" + password + "*";
        String pattern5 = "LOCK *" + password + "*";
        String pattern6 = "RESETPASSWORD *" + password + "*";

        if(pattern1.equalsIgnoreCase(str))
        { 
            return 1;
        }
        else if(pattern2.equalsIgnoreCase(str))
        {
            return 2;
        }
return 0;
    }                        
    }

错误日志:

10-11 12:59:54.842: E/AndroidRuntime(18743): FATAL EXCEPTION: main
10-11 12:59:54.842: E/AndroidRuntime(18743): java.lang.RuntimeException: Unable to start receiver iiu.fyp.cdsr.SmsReceiver: java.lang.NullPointerException
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2379)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.access$1500(ActivityThread.java:149)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.os.Looper.loop(Looper.java:153)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.main(ActivityThread.java:5026)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at java.lang.reflect.Method.invokeNative(Native Method)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at java.lang.reflect.Method.invoke(Method.java:511)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at dalvik.system.NativeStart.main(Native Method)
10-11 12:59:54.842: E/AndroidRuntime(18743): Caused by: java.lang.NullPointerException
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.DBOperations.getUserPasswordsString(DBOperations.java:149)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.SmsReceiver.getMsgPattern(SmsReceiver.java:89)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.SmsReceiver.onReceive(SmsReceiver.java:31)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2372)
10-11 12:59:54.842: E/AndroidRuntime(18743):    ... 10 more

我需要在收到某些短信时调用活动并在单词后将其从收件箱中删除...对于 abortbroadcast() 我已将其优先级设置为高于短信服务..实际问题在哪里?有人可以纠正吗?

编辑:

以下是我从 sqlite 检索密码的数据库代码:

    public String[] getUserPasswordsString() {
                List PasswordClass = new ArrayList();

                Cursor cursor = database.query(DataBaseWrapper.PasswordTable,
                        PASSWORD_TABLE_COLUMNS, null, null, null, null, null);

                cursor.moveToFirst();
                String[ ] password = new String[1] ;
                int i=0;
                while (!cursor.isAfterLast()) {
                    PasswordClass sim = parsePasswords(cursor);
                    password[i] = sim.getUserPassword().toString();
                    i++;
                    PasswordClass.add(sim);
                    cursor.moveToNext();
                }


                cursor.close();
                return password;
            }
private PasswordClass parsePasswords(Cursor cursor) {
            PasswordClass UserPassword = new PasswordClass();
            UserPassword.setUserPasswordId((cursor.getInt(0)));
            UserPassword.setUserPassword(cursor.getString(1));
            return UserPassword;
        }
4

2 回答 2

1

你的有错误

String[] valuesPassword = DataDBoperation.getUserPasswordsString();

is null这DataDBoperation.getUserPasswordsString()就是它抛出 NullPointerException 的原因。

我认为您的问题出在您的 DataDBoperation getUserPasswordsString() 方法上。

于 2013-10-11T08:09:42.053 回答
0

您是否在清单中添加了权限。你应该添加

<uses-permission android:name="android.permission.RECEIVE_SMS"> 

如果还有问题请点击以下链接

如何在 Android 中接收短信

于 2013-10-11T09:40:50.407 回答