java.lang.NullPointerException...崩溃报告

标签 java android

我又遇到这个问题了。 In this thread我也遇到了与按钮点击声音相同的问题。没有任何帮助,所以我禁用了那个声音。没有其他解决方案。现在,我有相同的崩溃报告,但现在在另一个带有复选框声音的 Activity 中。我勒个去???在我禁用其他声音之前,我从未遇到过这种崩溃。所以我想如果我禁用这个声音,其他一些声音也会遇到这个问题。所以这不是解决方案。这是崩溃报告:

java.lang.NullPointerException
at rs.androidaplikacije.zastaveigradovi.Izbor.onCheckedChanged(Izbor.java:126)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
at android.widget.RadioGroup.access$600(RadioGroup.java:52)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
at android.widget.CompoundButton.toggle(CompoundButton.java:86)
at android.widget.RadioButton.toggle(RadioButton.java:72)
at android.widget.CompoundButton.performClick(CompoundButton.java:98)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3821)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)

和 Activity 代码:

    public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(music == true){
            buttonClicks.start();
            }
            }

此行有错误:

buttonClicks.start();

这是全类同学:

public class Izbor extends Activity implements OnClickListener, OnCheckedChangeListener{

    public boolean music;
    MediaPlayer buttonClicks;
    MediaPlayer buttonBack;
    RadioButton rbDesetEasy,rbDvadesetEasy,rbDesetMedium,rbDvadesetMedium,rbDesetHard, rbDvadesetHard;
    Button dNazad, dStart;
    RadioGroup rGrupa;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        music = getPrefs.getBoolean("checkbox", true);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.izbor);
        addListenerOnButton();

    }

    private void addListenerOnButton() {

        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        Typeface pitanjeVrh = Typeface.createFromAsset(getAssets(), "Lobster.ttf");


        rbDesetEasy = (RadioButton) findViewById(R.id.radio1);
        rbDvadesetEasy = (RadioButton) findViewById(R.id.radio2);
        rbDesetMedium = (RadioButton) findViewById(R.id.radio3);
        rbDvadesetMedium = (RadioButton) findViewById(R.id.radio4);
        rbDesetHard = (RadioButton) findViewById(R.id.radio5);
        rbDvadesetHard = (RadioButton) findViewById(R.id.radio6);
        dNazad = (Button) findViewById(R.id.bNazad);
        dStart = (Button) findViewById(R.id.bStart);
        rGrupa = (RadioGroup) findViewById(R.id.radioGroup1);
        buttonBack = MediaPlayer.create(Izbor.this, R.raw.button31);
        tv1 = (TextView) findViewById(R.id.tv1);
        buttonClicks = MediaPlayer.create(this, R.raw.click);

        dNazad.setTypeface(dugmad);
        dStart.setTypeface(dugmad);
        rbDesetEasy.setTypeface(dugmad);
        rbDvadesetEasy.setTypeface(dugmad);
        rbDesetMedium.setTypeface(dugmad);
        rbDvadesetMedium.setTypeface(dugmad);
        rbDesetHard.setTypeface(dugmad);
        rbDvadesetHard.setTypeface(dugmad);
        tv1.setTypeface(pitanjeVrh);


        rGrupa.setOnCheckedChangeListener(this);
        rbDesetEasy.setOnClickListener(this);
        rbDvadesetEasy.setOnClickListener(this);
        rbDesetMedium.setOnClickListener(this);
        rbDvadesetMedium.setOnClickListener(this);
        rbDesetHard.setOnClickListener(this);
        rbDvadesetHard.setOnClickListener(this);
        dStart.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if(rbDesetEasy.isChecked()){
                    Intent intent = new Intent(Izbor.this, Kviz.class);
                    intent.putExtra("myMethod", "nextQuestion");
                    startActivity(intent);
                }else if(rbDvadesetEasy.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDesetMedium.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz10Medium.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDvadesetMedium.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20Medium.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDesetHard.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz10Hard.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDvadesetHard.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20Hard.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                }   
            }
        });


        dNazad.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(music == true){
                buttonBack.start();
                }
                finish();   
            }
        });
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(music == true){
        buttonClicks.start();
        }
        }

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

最佳答案

即使您尝试在此处实例化您的 MediaPlayer,它仍显示为 null

R.raw.click

根据Docs

Returns a MediaPlayer object, or null if creation failed

因此这告诉我们您的 R.raw.clicks 可能丢失、拼写错误或以其他方式不可用。这导致 MediaPlayer 创建失败,因此将 buttonClicks 保留为 null

关于java.lang.NullPointerException...崩溃报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16617025/

相关文章:

java - java中使用数组实现堆栈的切换情况

java - 如何在 Spring Boot 中进行多个 URL 映射(别名)

javascript - 在 javascript 中访问/生成 java 枚举

android - Coroutine 的 Deferred 类型为 null 会怎样?

android - 在 webview 中播放 youtube 视频并且 webview 正在使用时,“如果播放没有立即开始,请尝试重新启动您的设备”

java - 使用正则表达式搜索 HashSet

java - 如何从 ifPresentOrElse 语句中获取对象数据?

android - Broadcastreceiver 发出的通知在被反压时关闭应用程序

android - Cocos2dx - android apk 在启动时崩溃

android - 如何使用观察者更新我的 UI?