音频管理器空指针服务中的异常



我需要帮助,我发现了另一个相同的问题,但对我不起作用。

这是我的代码:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CallDetector extends Service {
    static String[] test = new String[15];
    boolean isstart = false;
    AudioManager audio_mng;
        private static final String DEBUG_TAG = "InviteActivity";
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CallDetector calldetector= new CallDetector(this);
        Log.v(DEBUG_TAG, "Service start");
        int i = 0;
        File file = new File("pathtofile/myfile.txt");
        try {
            Scanner scaner = new Scanner(new FileReader(file));
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            do{
                line = scaner.nextLine();
                test[i] = line;
                i++;
                br.close();
            }while(scaner.hasNext());
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }
        calldetector.start();
        return START_STICKY;
    }
    public void onDestroy(){
        CallDetector calldetector = new CallDetector(this);
        calldetector.stop();
        super.onDestroy();
    }
    public void onCreate(){
        super.onCreate();
        audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
    }
        private class PhoneCallListener extends PhoneStateListener {
            private boolean isPhoneCalling = false;
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (TelephonyManager.CALL_STATE_RINGING == state) {
                    for(int i =0; i <15; i++) {
                        if (incomingNumber.equals(test[i])) {
                            Log.v(DEBUG_TAG, "OK");
                            onchangetonormal();
                        }
                    }
                }
            }
        }
        private Context ctx;
        private TelephonyManager tm;
        private PhoneCallListener callStateListener;
        public void onchangetonormal(){
            audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        public CallDetector(Context ctx) {
            this.ctx = ctx;
            callStateListener = new PhoneCallListener();
        }
        /**
         * Start calls detection.
         */
        public void start() {
            Log.v(DEBUG_TAG, "Start listening");
            tm = (TelephonyManager)     ctx.getSystemService(Context.TELEPHONY_SERVICE);
            tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
        /**
         * Stop calls detection.
         */
        public void stop() {
                tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        }
    }

当我接到服务正在运行的呼叫时,我收到错误,但服务继续运行。

在我的第一个代码中是这样的:

if (TelephonyManager.CALL_STATE_RINGING == state) {
                    for(int i =0; i <15; i++) {
                        if (incomingNumber.equals(test[i])) {
                            Log.v(DEBUG_TAG, "OK");
AudioManager audio_mng;
audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        }
                    }
                }

一切都在电话接到电话的时候!正如我所说,我找到了一个解决方案,我尝试将它们放入 onCreat 中,然后调用它,如果什么都没有。

编辑:

FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.test.testapp.CallDetector.onchangetonormal(CallDetector.java:156)
            at com.test.testapp.CallDetector$PhoneCallListener.onCallStateChanged(CallDetector.java:107)
            at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)

谢谢!

对不起,我的英语不好!

与日志NullPointerException一样,onchangetonormal方法中发生异常意味着audio_mng null

在方法中使用audio_mng对象之前先进行空检查onchangetonormal

if(audio_mng==null){
  audio_mng=(AudioManager)CallDetector.this.getSystemService(
                                                      Context.AUDIO_SERVICE);
}

我今天有一个想法....将音频管理器audio_mng声明为静态...。那行得通!

如果您有其他解释或其他答案或原因,请回答。

感谢您的关注

相关内容

最新更新