【问题标题】:Audio manager nullpointerexception in service服务中的音频管理器空指针异常
【发布时间】:2015-02-02 22:43:37
【问题描述】:

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

这是我的代码:

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 中,然后在 if 但没有任何内容时调用它。

编辑:

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)

谢谢!

对不起我的英语不好!

【问题讨论】:

  • 您遇到了什么错误?如果应用程序崩溃,请添加带有问题的日志
  • 我添加了错误。是的,它崩溃了,但我想重新开始

标签: android service nullpointerexception android-service android-audiomanager


【解决方案1】:

如日志中的NullPointerException 异常发生在onchangetonormal 方法意味着audio_mngnull

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

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

【讨论】:

  • nothing...这条线在 onCreate 所以我把它放在这里。我也试试这个 if(audio_mng == null) { Log.v(DEBUG_TAG, "audiomanager" + audio_mng); } 在 onCreat 所以如果它是 null 它会显示消息。但它没有显示任何东西,所以它不是空的。
  • @NinjTsax:当您在onchangetonormal 方法中使用建议代码时会发生什么?
  • 我不明白你要我做什么。但我考虑权限。我已经拥有电话状态权限,但它可能的服务需要单独的权限?
【解决方案2】:

我今天有个主意.... 将 AudioManager audio_mng 声明为静态.... 这行得通!

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

感谢您的关注

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多