有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓中的java空指针异常错误

我在安卓中运行texter应用程序时遇到以下异常

堆栈跟踪:

java.lang.NullPointerException at com.texter.messenger.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:116) at 安卓.os.Handler.dispatchMessage(Handler.java:99) at 安卓.os.Looper.loop(Looper.java:123) at 安卓.os.HandlerThread.run(HandlerThread.java:60) , PHONE_MODEL : GT-I9000 , ANDROID_VERSION : 2.2 , APP_VERSION_CODE : 10 30.Nov.2011 00:33:50 AM , CUSTOM_DATA : , STACK_TRACE : java.lang.IllegalArgumentException: Receiver not registered: 安卓.widget.ViewFlipper$1@40597cc0 at 安卓.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:634) at 安卓.app.ContextImpl.unregisterReceiver(ContextImpl.java:881) at 安卓.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331) at 安卓.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104) at 安卓.view.View.dispatchDetachedFromWindow(View.java:6235) at 安卓.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1250) at 安卓.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248) at 安卓.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248) at 安卓.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248) at 安卓.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1838) at 安卓.view.ViewRoot.doDie(ViewRoot.java:2916) at 安卓.view.ViewRoot.die(ViewRoot.java:2886) at 安卓.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:254) at 安卓.view.Window$LocalWindowManager.removeViewImmediate(Window.java:445) at 安卓.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3182) at 安卓.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3287) at 安卓.app.ActivityThread.access$1600(ActivityThread.java:132) at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1042) at 安卓.os.Handler.dispatchMessage(Handler.java:99) at 安卓.os.Looper.loop(Looper.java:150) at 安卓.app.ActivityThread.main(ActivityThread.java:4293) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method).

我对这些错误一无所知。如何解决这些错误

package com.texter.messenger;

import java.util.List;

import 安卓.app.Activity;
import 安卓.app.PendingIntent;
import 安卓.app.PendingIntent.CanceledException;
import 安卓.app.Service;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.content.pm.PackageManager;
import 安卓.content.pm.ResolveInfo;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.os.HandlerThread;
import 安卓.os.IBinder;
import 安卓.os.Looper;
import 安卓.os.Message;
import 安卓.os.PowerManager;
import 安卓.os.Process;
import 安卓.telephony.SmsManager;
import 安卓.telephony.SmsMessage;
import 安卓.telephony.SmsMessage.MessageClass;
import 安卓.telephony.TelephonyManager;
import 安卓.util.Log;

import com.texter.common.TexterConstants;
import com.texter.common.TexterNotification;
import com.texter.common.Utils;
import com.texter.data.TexterDB;
import com.texter.data.TexterDB.Schedule;
import com.texter.preferences.TexterPreferenceManager;
import com.texterpro.app.ConversationList;
import com.texterpro.app.R;
import com.texterpro.app.SmsPopupView;

public class SmsReceiverService extends Service {

        private static final String LOG_TAG = TexterConstants.COMMON_TAG;

        private static final String ACTION_SMS_RECEIVED = "安卓.provider.Telephony.SMS_RECEIVED";
        private static final String ACTION_MMS_RECEIVED = "安卓.provider.Telephony.WAP_PUSH_RECEIVED";
        private static final String ACTION_MESSAGE_RECEIVED = "net.everything安卓.smspopup.MESSAGE_RECEIVED";
        private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";

        // https://安卓.googlesource.com/platform/packages/apps/Mms/+/master/src/com/安卓/mms/transaction/SmsReceiverService.java
        public static final String MESSAGE_SENT_ACTION = "com.安卓.mms.transaction.MESSAGE_SENT";

        /*
         * This is the number of retries and pause between retries that we will keep
         * checking the system message database for the latest incoming message
         */
        private static final int MESSAGE_RETRY = 8;
        private static final int MESSAGE_RETRY_PAUSE = 1000;

        private Context context;
        private ServiceHandler mServiceHandler;
        private Looper mServiceLooper;
        private int mResultCode;

        private static final Object mStartingServiceSync = new Object();
        private static PowerManager.WakeLock mStartingService;

        private static final int TOAST_HANDLER_MESSAGE_SENT = 0;
        private static final int TOAST_HANDLER_MESSAGE_SEND_LATER = 1;
        private static final int TOAST_HANDLER_MESSAGE_FAILED = 2;

        @Override
        public void onCreate() {
                HandlerThread thread = new HandlerThread("Log.LOGTAG",
                                Process.THREAD_PRIORITY_BACKGROUND);
                thread.start();
                context = getApplicationContext();
                mServiceLooper = thread.getLooper();
                mServiceHandler = new ServiceHandler(mServiceLooper);
                Log.v(LOG_TAG, "Oncreate");
        }

        @Override
        public void onStart(Intent intent, int startId) {
                Log.v(LOG_TAG, "OnStart");
                mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0;

                Message msg = mServiceHandler.obtainMessage();
                msg.arg1 = startId;
                msg.obj = intent;
                mServiceHandler.sendMessage(msg);
        }

        @Override
        public void onDestroy() {
                mServiceLooper.quit();
        }

        @Override
        public IBinder onBind(Intent intent) {
                return null;
        }

        private final class ServiceHandler extends Handler {
                public ServiceHandler(Looper looper) {
                        super(looper);
                }

                @Override
                public void handleMessage(Message msg) {
                        Log.v(LOG_TAG, "handlemessage:");
                        Log.v(LOG_TAG, msg.toString());
                        int serviceId = msg.arg1;
                        Intent intent = (Intent) msg.obj;
                        String action = intent.getAction();
                        String dataType = intent.getType();

                        if (ACTION_SMS_RECEIVED.equals(action)) {
                                handleSmsReceived(intent);
                        } else if (ACTION_MMS_RECEIVED.equals(action)
                                        && MMS_DATA_TYPE.equals(dataType)) {
                                handleMmsReceived(intent);
                        } else if (MESSAGE_SENT_ACTION.equals(action)) {
                                handleSmsSent(intent);
                        } else if (ACTION_MESSAGE_RECEIVED.equals(action)) {
                                handleMessageReceived(intent);
                        }

                        // NOTE: We MUST not call stopSelf() directly, since we need to
                        // make sure the wake lock acquired by AlertReceiver is released.
                        finishStartingService(SmsReceiverService.this, serviceId);
                }
        }

        /**
         * Handle receiving a SMS message
         */
        private void handleSmsReceived(Intent intent) {

                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                        SmsMessage[] messages = Utils.getMessagesFromIntent(intent);
                        if (messages != null) {
                                notifyMessageReceived(new SmsMmsMessage(context, messages,
                                                System.currentTimeMillis()));
                        }
                }
        }

        private void notifyMessageReceived(SmsMmsMessage message) {

                // Class 0 SMS, let the system handle this
                if (message.getMessageType() == SmsMmsMessage.MESSAGE_TYPE_SMS
                                && message.getMessageClass() == MessageClass.CLASS_0) {
                        return;
                }

                TelephonyManager mTM = (TelephonyManager) context
                                .getSystemService(Context.TELEPHONY_SERVICE);
                boolean callStateIdle = mTM.getCallState() == TelephonyManager.CALL_STATE_IDLE;

                /*
                 * If popup is enabled for this user -AND- the user is not in a call
                 * -AND- -AND- phone is not docked -AND- (screen is locked -OR- (setting
                 * is OFF to only show on keyguard -AND- user is not in messaging app:
                 * then show the popup activity, otherwise check if notifications are on
                 * and just use the standard notification))
                 */

                boolean isApp = TexterPreferenceManager.getInstance(this)
                                .isAppEnabled();
                boolean showPop = TexterPreferenceManager.getInstance(this)
                                .isPopupEnabled();
                boolean showNotify = TexterPreferenceManager.getInstance(this)
                                .isNotifyEnabled();

                // Log.v(LOG_TAG," App = "+ isApp );
                // Log.v(LOG_TAG," pop = "+showPop );
                // Log.v(LOG_TAG," notify = "+showNotify );

                // if conversationList is visible then do not show pop
                if (!ConversationList.isVisible()) {
                        if (isApp && callStateIdle && showPop) {
                                // Log.v(LOG_TAG," showing popup = " );
                                if (!SmsPopupView.isPopupVisible())
                                        context.startActivity(message.getPopupIntent());
                        }
                }

                if (isApp && showNotify) {
                        TexterNotification.ShowMessageNotification(this, message);
                }

        }

        /**
         * Handle receiving a MMS message
         */
        private void handleMmsReceived(Intent intent) {

        }

        /**
         * Handle receiving an arbitrary message (potentially coming from a 3rd
         * party app)
         */
        private void handleMessageReceived(Intent intent) {

                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                }
        }

        /*
         * Handler to deal with showing Toast messages for message sent status
         */
        public Handler mToastHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {

                        if (msg != null) {
                                switch (msg.what) {
                                case TOAST_HANDLER_MESSAGE_SENT:
                                        // TexterNotification.showToast(SmsReceiverService.this,R.string.quickreply_sent_toast);
                                        break;
                                case TOAST_HANDLER_MESSAGE_SEND_LATER:
                                        TexterNotification.showToast(SmsReceiverService.this,
                                                        R.string.quickreply_failed_send_later);
                                        break;
                                case TOAST_HANDLER_MESSAGE_FAILED:
                                        TexterNotification.showToast(SmsReceiverService.this,
                                                        R.string.quickreply_failed);
                                        break;
                                }
                        }
                }
        };

        /*
         * Handle the result of a sms being sent
         */
        private void handleSmsSent(Intent intent) {

                Log.v(LOG_TAG, "HandleSMSSent called");
                PackageManager pm = getPackageManager();
                Intent sysIntent = null;
                Intent tempIntent;
                List<ResolveInfo> receiverList;
                boolean forwardToSystemApp = true;

                // Search for system messaging app that will receive our
                // "message sent complete" type intent
                tempIntent = intent.setClassName(
                                SmsMessageSender.MESSAGING_PACKAGE_NAME,
                                SmsMessageSender.MESSAGING_RECEIVER_CLASS_NAME);

                tempIntent.setAction(SmsReceiverService.MESSAGE_SENT_ACTION);

                receiverList = pm.queryBroadcastReceivers(tempIntent, 0);

                if (receiverList.size() > 0) {
                        sysIntent = tempIntent;
                }

                Bundle b = intent.getExtras();
                long rowid = 0;

                rowid = b == null ? 0 : b.getLong("ROWID");

                /*
                 * No system messaging app was found to forward this intent to,
                 * therefore we will need to do the final piece of this ourselves which
                 * is basically moving the message to the correct folder depending on
                 * the result.
                 */
                // TexterNotification.showToast(SmsReceiverService.this,
                // "before moving folder");
                if (sysIntent == null) {

                        forwardToSystemApp = false;
                        Uri uri = intent.getData();
                        Log.v(LOG_TAG, "id = " + rowid);
                        if (mResultCode == Activity.RESULT_OK) {
                                SmsMessageSender.moveMessageToFolder(this, uri,
                                                SmsMessageSender.MESSAGE_TYPE_SENT);
                        } else if ((mResultCode == SmsManager.RESULT_ERROR_RADIO_OFF)
                                        || (mResultCode == SmsManager.RESULT_ERROR_NO_SERVICE)) {
                                SmsMessageSender.moveMessageToFolder(this, uri,
                                                SmsMessageSender.MESSAGE_TYPE_QUEUED);
                        } else {
                                SmsMessageSender.moveMessageToFolder(this, uri,
                                                SmsMessageSender.MESSAGE_TYPE_FAILED);
                        }
                }

                // Check the result and notify the user using a toast
                if (mResultCode == Activity.RESULT_OK) {
                        mToastHandler.sendEmptyMessage(TOAST_HANDLER_MESSAGE_SENT);
                        TexterDB.getInstance().updateStatus(rowid,
                                        Schedule.STATUS_SCHEDULED_SENT, 0);
                } else if ((mResultCode == SmsManager.RESULT_ERROR_RADIO_OFF)
                                || (mResultCode == SmsManager.RESULT_ERROR_NO_SERVICE)) {
                        TexterDB.getInstance().updateStatus(rowid,
                                        Schedule.STATUS_NOT_SENT, -1);
                } else {
                        mToastHandler.sendEmptyMessage(TOAST_HANDLER_MESSAGE_FAILED);
                        TexterDB.getInstance().updateStatus(rowid,
                                        Schedule.STATUS_NOT_SENT, mResultCode);
                }

                if (forwardToSystemApp) {
                        try {
                                PendingIntent.getBroadcast(this, 0, sysIntent, 0).send(
                                                mResultCode);
                        } catch (CanceledException e) {
                                e.printStackTrace();
                        }
                }
        }

        /**
         * Start the service to process the current event notifications, acquiring
         * the wake lock before returning to ensure that the service will run.
         */
        public static void beginStartingService(Context context, Intent intent) {
                synchronized (mStartingServiceSync) {
                        if (mStartingService == null) {
                                PowerManager pm = (PowerManager) context
                                                .getSystemService(Context.POWER_SERVICE);
                                mStartingService = pm.newWakeLock(
                                                PowerManager.PARTIAL_WAKE_LOCK,
                                                "Texter.SmsReceiverService");
                                mStartingService.setReferenceCounted(false);
                        }
                        mStartingService.acquire();
                        context.startService(intent);
                        Log.v(LOG_TAG, "service started");
                }
        }

        /**
         * Called back by the service when it has finished processing notifications,
         * releasing the wake lock if the service is now stopping.
         */
        public static void finishStartingService(Service service, int startId) {
                synchronized (mStartingServiceSync) {
                        if (mStartingService != null) {
                                if (service.stopSelfResult(startId)) {
                                        mStartingService.release();
                                }
                        }
                }
        }

}

共 (3) 个答案

  1. # 1 楼答案

    我也有同样的错误,仍然在寻找正确的解决方案,但暂时我只是注释掉onDetachFromWindow()方法。在我的情况下,这是为了测试的目的,所以评论它没有影响我,但不确定你的情况下,但给它一个尝试

  2. # 2 楼答案

    一般来说,这个问题/答案介绍了如何阅读这些错误:
    What is a stack trace, and how can I use it to debug my application errors?

    看看代码,intent不应该是null,毕竟intent.getAction()执行时没有错误
    似乎更可能是ViewFlipper有问题,在嵌套堆栈跟踪中获得一些"Receiver not registered" exceptionjava.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@40597cc0

    也许这个问题可以为你指明正确的方向:
    ViewFlipper : Receiver not registered

  3. # 3 楼答案

    看看痕迹的顶部。。。看看下面的代码:

    com.texter.messenger.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:116)
    

    看看代码。。。在调用handleSmsReceived之前,检查变量intent是否不是null