在Click Listener上突然终止应用程序

时间:2012-07-05 02:17:05

标签: android button onclicklistener

我有一个带OnClickListener()的文本框。每当我在文本框中键入字母时,它都会使用一些声音文件大声朗读。 问题是,在读出/打字后说5-6个字母表后,应用程序意外终止。我无法弄清楚原因。为什么会这样?

代码在这里:

package com.example.newaa;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

import android.os.Bundle;

public class Recipient extends Activity{
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recipient);
            EditText a;
            a=(EditText)findViewById(R.id.editText3);
            a.addTextChangedListener(new TextWatcher(){ 
                @Override   
                public void afterTextChanged(Editable s) {   
                       //GET TEXT HERE AFTER TYPING DONE BY PERSON   

                    }   
                @Override   
                public void beforeTextChanged(CharSequence s,int start,int count,int after){  
                      //GET TEXT HERE BEFORE TYPING START BY PERSON

                    }   
                @SuppressLint("ParserError")
                @Override   
                public void onTextChanged(CharSequence s, int start, int before, int count) {   
                   //GET TYPED CHAR HERE 

                    char ch=s.charAt(s.length()-1);
                    if(ch=='a')
                    {

                        MediaPlayer mPlayer1 = MediaPlayer.create(Recipient.this, R.raw.a);
                        mPlayer1.start();
                    }
                    if(ch=='b')
                    {
                        MediaPlayer mPlayer2 = MediaPlayer.create(Recipient.this, R.raw.b);
                        mPlayer2.start();
                    }
                    if(ch=='c')
                    {
                        MediaPlayer mPlayer3 = MediaPlayer.create(Recipient.this, R.raw.c);
                        mPlayer3.start();
                    }
                    if(ch=='d')
                    {
                        MediaPlayer mPlayer4 = MediaPlayer.create(Recipient.this, R.raw.d);
                        mPlayer4.start();
                    }
                    if(ch=='e')
                    {
                        MediaPlayer mPlayer5 = MediaPlayer.create(Recipient.this, R.raw.e);
                        mPlayer5.start();
                    }
                    if(ch=='f')
                    {
                        MediaPlayer mPlayer6 = MediaPlayer.create(Recipient.this, R.raw.f);
                        mPlayer6.start();
                    }
                    if(ch=='g')
                    {
                        MediaPlayer mPlayer7 = MediaPlayer.create(Recipient.this, R.raw.g);
                        mPlayer7.start();
                    }
                    if(ch=='h')
                    {
                        MediaPlayer mPlayer8 = MediaPlayer.create(Recipient.this, R.raw.h);
                        mPlayer8.start();
                    }
                    if(ch=='i')
                    {
                        MediaPlayer mPlayer9 = MediaPlayer.create(Recipient.this, R.raw.i);
                        mPlayer9.start();
                    }
                    if(ch=='j')
                    {
                        MediaPlayer mPlayer10 = MediaPlayer.create(Recipient.this, R.raw.j);
                        mPlayer10.start();
                    }
                    if(ch=='k')
                    {
                        MediaPlayer mPlayer11 = MediaPlayer.create(Recipient.this, R.raw.k);
                        mPlayer11.start();
                    }
                    if(ch=='l')
                    {
                        MediaPlayer mPlayer12 = MediaPlayer.create(Recipient.this, R.raw.l);
                        mPlayer12.start();
                    }
                    if(ch=='m')
                    {
                        MediaPlayer mPlayer13 = MediaPlayer.create(Recipient.this, R.raw.m);
                        mPlayer13.start();
                    }
                    if(ch=='n')
                    {
                        MediaPlayer mPlayer14 = MediaPlayer.create(Recipient.this, R.raw.n);
                        mPlayer14.start();
                    }
                    if(ch=='o')
                    {
                        MediaPlayer mPlayer15 = MediaPlayer.create(Recipient.this, R.raw.o);
                        mPlayer15.start();
                    }
                    if(ch=='p')
                    {
                        MediaPlayer mPlayer16 = MediaPlayer.create(Recipient.this, R.raw.p);
                        mPlayer16.start();
                    }
                    if(ch=='q')
                    {
                        MediaPlayer mPlayer17 = MediaPlayer.create(Recipient.this, R.raw.q);
                        mPlayer17.start();
                    }
                    if(ch=='r')
                    {
                        MediaPlayer mPlayer18 = MediaPlayer.create(Recipient.this, R.raw.r);
                        mPlayer18.start();
                    }
                    if(ch=='s')
                    {
                        MediaPlayer mPlayer19 = MediaPlayer.create(Recipient.this, R.raw.s);
                        mPlayer19.start();
                    }
                    if(ch=='t')
                    {
                        MediaPlayer mPlayer20 = MediaPlayer.create(Recipient.this, R.raw.t);
                        mPlayer20.start();
                    }
                    if(ch=='u')
                    {
                        MediaPlayer mPlayer21 = MediaPlayer.create(Recipient.this, R.raw.u);
                        mPlayer21.start();
                    }
                    if(ch=='v')
                    {
                        MediaPlayer mPlayer22 = MediaPlayer.create(Recipient.this, R.raw.v);
                        mPlayer22.start();
                    }
                    if(ch=='w')
                    {
                        MediaPlayer mPlayer23 = MediaPlayer.create(Recipient.this, R.raw.w);
                        mPlayer23.start();
                    }
                    if(ch=='x')
                    {
                        MediaPlayer mPlayer24 = MediaPlayer.create(Recipient.this, R.raw.x);
                        mPlayer24.start();
                    }
                    if(ch=='y')
                    {
                        MediaPlayer mPlayer25 = MediaPlayer.create(Recipient.this, R.raw.y);
                        mPlayer25.start();
                    }
                    if(ch=='z')
                    {
                        MediaPlayer mPlayer26 = MediaPlayer.create(Recipient.this, R.raw.z);
                        mPlayer26.start();
                    }
                  }   
                });
     }

}

logcat在这里:

07-05 08:08:42.153: E/AndroidRuntime(321): Uncaught handler: thread main exiting due to uncaught exception
07-05 08:08:42.183: E/AndroidRuntime(321): java.lang.NullPointerException
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.example.newaa.Recipient$1.onTextChanged(Recipient.java:79)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.widget.TextView.handleTextChanged(TextView.java:6172)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:578)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:231)
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:57)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)
07-05 08:08:42.183: E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 08:08:42.183: E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 08:08:42.183: E/AndroidRuntime(321):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 08:08:42.183: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 08:08:42.183: E/AndroidRuntime(321):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

EDITED PART .....

一旦声音是播放器,尝试释放媒体播放器 ...并且无需创建媒体播放器的多个实例...在开始时重新初始化相同的实例..

答案 1 :(得分:0)

由于将Illegal Activity Context (NULL Context)传递给任何方法或其他活动而导致错误。

请参阅具有类似问题的Uncaught handler: thread main exiting due to uncaught exception。你可以找出问题所在。