Erro

5 respostas
D

primeiramente Olá! Alguém pode me dizer porque estou tendo este erro?!

02-01 18:23:06.558: E/AndroidRuntime(1255): FATAL EXCEPTION: main

02-01 18:23:06.558: E/AndroidRuntime(1255): java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at visao.comunicador.FalarV.onClick(FalarV.java:57)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.view.View.performClick(View.java:3100)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.view.View$PerformClick.run(View.java:11644)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.os.Handler.handleCallback(Handler.java:587)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.os.Handler.dispatchMessage(Handler.java:92)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.os.Looper.loop(Looper.java:126)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at android.app.ActivityThread.main(ActivityThread.java:3997)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at java.lang.reflect.Method.invokeNative(Native Method)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at java.lang.reflect.Method.invoke(Method.java:491)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)

02-01 18:23:06.558: E/AndroidRuntime(1255): 	at dalvik.system.NativeStart.main(Native Method)

5 Respostas

rodrigo_ror
Normalmente vc declara uma coisa e tenta usar de outra forma. Verifique seus EditText.. se nao achar o erro poste aí o codigo pra ver se da pra ajudar.

acabei de receber esse erro: havia declarado um TextView e tentei usar como EditText.

 :D

Normalmente vc declara uma coisa e tenta usar de outra forma. Verifique seus EditText… se nao achar o erro poste aí o codigo pra ver se da pra ajudar.

acabei de receber esse erro: havia declarado um TextView e tentei usar como EditText.

:smiley:

D

Muito Obrigado! eu consegui resolver esse problema.

Porém me apareceu outro.

Olhem essa clase, que implementa um Text-to-speech, que eu achei pronta.

package comunicador;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;
import android.widget.Toast;
 
public class Falar extends Activity implements OnInitListener {
 
        //Objeto TTS
    public TextToSpeech myTTS;
    
        //Código de status
    private int MY_DATA_CHECK_CODE = 0;
 
    public void onCreate(Bundle savedInstanceState) {
 
            //check for TTS data
            Intent checkTTSIntent = new Intent();
            checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
            startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
    }
 
 
       //falar o texto
    public void speakWords(String speech) {
 
            //fala o texto imediatamente
            myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }
 
    //act on result of TTS data check
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            //the user has the necessary data - create the TTS
        myTTS = new TextToSpeech(this, this);
        }
        else {
                //no data - install it now
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}
        //setup 
    public void onInit(int initStatus) {
 
            //checar o sucesso da operação
        if (initStatus == TextToSpeech.SUCCESS) {
            if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        }
        //em caso de falha, exibir a mensagem
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Desculpe! a sintetização de voz falhou. ", Toast.LENGTH_LONG).show();
        }
    }
}

Tô tentando fazer na minha outra classe assim:

public void onClick(View v) {
    		switch (v.getId()) {
			case R.id.falar_button:
				enteredText = (EditText)findViewById(R.id.editText1);
	                        words = enteredText.getText().toString();
	                        Falar text = new Falar();
	                        text.speakWords(words);
				break;
Isso tá me retornando um
java.lang.NullPointerException

O que eu estou fazendo de errado?! deve ser algo bem simples, que está me escapando.
Grato. :)

rodrigo_ror

Opa,
Só posta como vc resolveu, ou o que era o problema… pra poder ajudar outros usuários que passarem pelo mesmo problema.

E No novo erro, tenta postar ele completo pra gente tentar te ajudar.

flw

D

Era simples! Estava tentando usar um button como editText, assim:

aí só mudei para:

Quanto ao novo erro, segue o que apareceu no Logcat:

02-02 12:21:02.686: E/AndroidRuntime(1098): FATAL EXCEPTION: main
02-02 12:21:02.686: E/AndroidRuntime(1098): java.lang.NullPointerException
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:451)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at comunicador.Falar.speakWords(Falar.java:23)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at visao.comunicador.FalarV.onClick(FalarV.java:49)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.view.View.performClick(View.java:3100)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.view.View$PerformClick.run(View.java:11644)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.os.Handler.handleCallback(Handler.java:587)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.os.Looper.loop(Looper.java:126)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at android.app.ActivityThread.main(ActivityThread.java:3997)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at java.lang.reflect.Method.invoke(Method.java:491)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-02 12:21:02.686: E/AndroidRuntime(1098): 	at dalvik.system.NativeStart.main(Native Method)

Desde já agradeço!

D

Será que alguém podia pelo menos tentar me responder isso, até hoje não consegui fazer, nem entender o porque desse erro.

Grato, :slight_smile:

Criado 1 de fevereiro de 2012
Ultima resposta 3 de fev. de 2012
Respostas 5
Participantes 2