当前位置: 代码迷 >> java >> 在eclipse android real设备上运行我的程序时,我在LogCat中遇到许多错误,这些错误以及如何解决?
  详细解决方案

在eclipse android real设备上运行我的程序时,我在LogCat中遇到许多错误,这些错误以及如何解决?

热度:54   发布时间:2023-08-02 10:41:18.0

不幸的是,在我的智能手机上,我看到一条消息,提示该应用已停止。

然后我在LogCat中看到了以下错误:

07-19 14:32:48.438: D/ContextHelper(9232): convertTheme. context->name=com.example.texttospeechapp themeResourceId=2131427621
07-19 14:32:48.455: I/PhoneWindow(9232): [generateLayout] setColorNavigationBar => color=0x ff000001
07-19 14:32:48.470: D/PhoneWindowEx(9232): [LMJ][PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000
07-19 14:32:48.470: I/PhoneWindow(9232): [setNavigationBarColor2] color=0x ff000000
07-19 14:32:48.603: D/AndroidRuntime(9232): Shutting down VM
07-19 14:32:48.603: D/AndroidRuntime(9232): --------- beginning of crash
07-19 14:32:48.605: E/AndroidRuntime(9232): FATAL EXCEPTION: main
07-19 14:32:48.605: E/AndroidRuntime(9232): Process: com.example.texttospeechapp, PID: 9232
07-19 14:32:48.605: E/AndroidRuntime(9232): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.texttospeechapp/com.example.texttospeechapp.MainActivity}: java.lang.ClassCastException: com.example.texttospeechapp.MainActivity cannot be cast to android.speech.tts.TextToSpeech$OnInitListener
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread.access$800(ActivityThread.java:148)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.os.Looper.loop(Looper.java:135)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread.main(ActivityThread.java:5274)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invoke(Native Method)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invoke(Method.java:372)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
07-19 14:32:48.605: E/AndroidRuntime(9232): Caused by: java.lang.ClassCastException: com.example.texttospeechapp.MainActivity cannot be cast to android.speech.tts.TextToSpeech$OnInitListener
07-19 14:32:48.605: E/AndroidRuntime(9232):     at com.example.texttospeechapp.MainActivity.onCreate(MainActivity.java:27)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.Activity.performCreate(Activity.java:5977)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-19 14:32:48.605: E/AndroidRuntime(9232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
07-19 14:32:48.605: E/AndroidRuntime(9232):     ... 10 more
07-19 14:32:54.720: I/Process(9232): Sending signal. PID: 9232 SIG: 9

这是我在MainActivity.java中的代码

package com.example.texttospeechapp;

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;

import java.util.Locale;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {




    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextToSpeech tts = new TextToSpeech(this,(OnInitListener) this);
        tts.setLanguage(Locale.US);
        tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的代码没有任何错误。 9条警告,但无错误。

解决:这是一个有效的代码:

package com.example.texttospeechapp;

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;

import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements OnInitListener {


    private static final int MY_DATA_CHECK_CODE = 0;
    TextToSpeech mTts;



    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        //tts = new TextToSpeech(this,(OnInitListener) this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initTTS();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initTTS() {
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == MY_DATA_CHECK_CODE) {
        if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        mTts = new TextToSpeech(this, this);
        } else {
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
            }
        }
    }
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
                    if(result == TextToSpeech.LANG_AVAILABLE
                       || result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                             mTts.speak("Start system", TextToSpeech.QUEUE_FLUSH, null);
                    }
        }
    }
}

==== 问题 ====

您在MainActivity的这一行上收到ClassCastException:

    TextToSpeech tts = new TextToSpeech(this,(OnInitListener) this);

那是因为您正在将this (对活动的引用)转换为OnInitListener。

通过应用强制转换,您已经告诉编译器this是OnInitListener的实例。 这就是为什么您没有得到编译错误的原因。 但是,将在运行时检查强制类型转换,当VM确定that 不是该接口的实例时,它将失败。 因此,出现以下LogCat消息:

java.lang.ClassCastException:com.example.texttospeechapp.MainActivity无法转换为android.speech.tts.TextToSpeech $ OnInitListener

==== 解决方案 ====

要解决此问题,您必须在活动中实现该接口,如下所示

public class MainActivity extends ActionBarActivity implements OnInitListener {

然后您将需要在活动中实现一个OnInitListner方法:

public void onInit (int status) {
   // This will be called when the text-to-speech engine has been initialized. 
}
  相关解决方案