简体   繁体   English

单击按钮时应用崩溃

[英]App crashing on button click

Whenever I click my button my app crashes. 每当我单击按钮时,我的应用程序就会崩溃。 What am I doing wrong? 我究竟做错了什么?

public void onClick(View v) {

            int primaryLength = PRIMARY.length;
            final String primaryText = PRIMARY[rnd.nextInt(primaryLength)];
            int secondaryLength = SECONDARY.length;
            final String secondaryText = SECONDARY[rnd.nextInt(secondaryLength)];
            final TextView textOne = (TextView)findViewById(R.id.textView1);
            final TextView textTwo = (TextView)findViewById(R.id.textView2);
            textOne.setText(primaryText);
            textTwo.setText(secondaryText);

            String primeT = primaryText;
            Uri media = Uri.parse(primeT);
            mpButtonOne = MediaPlayer.create(MainActivity.this, media);
            mpButtonOne.start();
            mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mpButtonOne) {
                    String primeS = secondaryText;
                    Uri media2 = Uri.parse(primeS);
                    mpButtonTwo = MediaPlayer.create(MainActivity.this, media2);
                    mpButtonTwo.start();
                    mpButtonTwo.setOnCompletionListener(new soundListener1());
                    {
                    }
                }

I want the randomly selected text that is displayed in textviews to be entered as the location for the mediaplayer sound files, the words are the names of the sound files in my raw folder. 我希望将在textviews中显示的随机选择的文本输入为mediaplayer声音文件的位置,这些单词是原始文件夹中声音文件的名称。 I'm a bit new and really not sure if this is the right way to go about it. 我有点新,而且真的不确定这是否是正确的解决方法。

09-11 16:50:40.128: E/AndroidRuntime(12531): FATAL EXCEPTION: main
09-11 16:50:40.128: E/AndroidRuntime(12531): java.lang.NullPointerException
09-11 16:50:40.128: E/AndroidRuntime(12531):    at com.spunktrunk.nastylittleman.MainActivity$1.onClick(MainActivity.java:62)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.view.View.performClick(View.java:2408)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.view.View$PerformClick.run(View.java:8816)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.os.Handler.handleCallback(Handler.java:587)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.os.Looper.loop(Looper.java:123)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at android.app.ActivityThread.main(ActivityThread.java:4669)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at java.lang.reflect.Method.invokeNative(Native Method)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at java.lang.reflect.Method.invoke(Method.java:521)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
09-11 16:50:40.128: E/AndroidRuntime(12531):    at dalvik.system.NativeStart.main(Native Method)
09-11 16:50:40.128: W/ActivityManager(237):   Force finishing activity com.spunktrunk.nastylittleman/.MainActivity

Depending on your url, calling Media.create might have returned a null value ( look at the documentation here ). 根据您的URL,调用Media.create可能返回了空值( 请参阅此处的文档 )。 If so, your code is trying to call a method on null, which causes your NPE (Null Pointer Exception) 如果是这样,则您的代码尝试在null上调用方法,这将导致您的NPE(空指针异常)

You could of course change your code like this: 您当然可以像这样更改代码:

 mpButtonOne = MediaPlayer.create(MainActivity.this, media);
 if (mButtonOne==null){
        //display a Toast message here
        return;
 }
 mpButtonOne.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM