本文介绍了getRingerMode()处的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个应用程序,用户可以通过单击图像按钮来切换静音模式:
package pa;

I wrote this app where users can toggle silent mode by clicking on an image button: package p.a;

import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        View.OnClickListener onClickListener = new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                mode.toggle(audioManager);
                update();
            }
        };
        imageView.setOnClickListener(onClickListener);
    }

    public void update(){
        int mod=mode.phonesilent(audioManager)?
                R.drawable.ringer_off:
                R.drawable.ringer_on;
        }
    }

这是模式类:

package p.a;

import android.media.AudioManager;

/**
 * Created by root on 9/19/17.
 */

public class mode {
    public static boolean phonesilent(AudioManager audioManager){
        return audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT;
    }

    public static void toggle(AudioManager audioManager){
        int mode = phonesilent(audioManager)?
                AudioManager.RINGER_MODE_NORMAL:
                AudioManager.RINGER_MODE_SILENT;
    }
}

该应用可以正常初始化,但是当我点击在图像图标上,它崩溃并在标题中生成错误消息。
这是logcat:

The app can be initialized normally, but when I click on the image icon, it crashes and produces the error message in the title.Here's the logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.AudioManager.getRingerMode()' on a null object reference
                                                         at p.a.mode.phonesilent(mode.java:11)
                                                         at p.a.MainActivity.update(MainActivity.java:29)
                                                         at p.a.MainActivity$1.onClick(MainActivity.java:22)


推荐答案

你可以就像Vamshi Krishna所说的那样,对于这个问题来说这是一个很好的答案,但是如果你不想因某种原因实例化它,可以将Mode class更改为Mode static class,如下所示:

You can do it as Vamshi Krishna says, it's totally a good answer for this problem, but if you don't want to instantiate it for some reason you could change "Mode class" to a "Mode static class" as follows:

package p.a;

import android.media.AudioManager;

/**
 * Created by root on 9/19/17.
 */

public static class mode {
    public static boolean phonesilent(AudioManager audioManager){
        return audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT;
    }
    public static void toggle(AudioManager audioManager){
        int mode = phonesilent(audioManager)?
                AudioManager.RINGER_MODE_NORMAL:
                AudioManager.RINGER_MODE_SILENT;
    }
}

希望有所帮助!

这篇关于getRingerMode()处的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:12