java - 当在 eclipse android 真实设备上运行我的程序时,我在 LogCat 中收到许多错误,所有这些错误是什么以及如何修复它们?

标签 java android eclipse

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

然后我在 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(对 Activity 的引用)转换为 OnInitListener。

通过应用强制转换,您已告知编译器 this 是 OnInitListener 的实例。这就是为什么您不会收到编译错误的原因。然而,转换会在运行时进行检查,当虚拟机确定不是该接口(interface)的实例时,转换就会失败。因此,出现以下 LogCat 消息:

java.lang.ClassCastException: com.example.texttospeechapp.MainActivity cannot be cast to android.speech.tts.TextToSpeech$OnInitListener

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

要解决此问题,您必须在 Activity 中实现该接口(interface),如下所示

public class MainActivity extends ActionBarActivity implements OnInitListener {

然后您需要在您的 Activity 中实现一个 OnInitListner 方法:

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

关于java - 当在 eclipse android 真实设备上运行我的程序时,我在 LogCat 中收到许多错误,所有这些错误是什么以及如何修复它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502040/

相关文章:

javac 如何用通配符编译整个源目录?

java - DataSnap Delphi XE3 和Android 4.0 没有返回选择功能?

android - Android SDK无法加载处理

java - 在 Android 中搜索带有关键字的任何网页

java - 在 toString() 中设置属性/实例变量和打印

java - ant构建脚本将参数传递给java任务

Java找不到类文件

java - ANDROID AsyncTask错误

android - 无法在Android模拟器中模拟位置数据

java - 如何向 MockMvc 添加文件和正文?