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

Audio manager nullpointerexception in service

我需要这方面的帮助,我又发现了一个相同的问题,但对我没有用。

那是我的代码:

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);
                        }
                    }

                }

当 phone 接到电话时,一切都在 if 中! 正如我所说,我找到了一个解决方案,我尝试将它们放在 onCreat 中,然后在 if but nothing 中调用它。

编辑:

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.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_mngnull

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

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

我今天有个想法.... 将 AudioManager audio_mng 声明为静态.... 行得通!

如果您有其他解释或其他答案或发生这种情况的原因,请回答。

感谢您的关注