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



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

然后我在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个警告但没有错误

Solved: This is a working code:

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);
                    }
        }
    }
}

= = = = 问题 = = = =

你得到一个ClassCastException在这一行的MainActivity:

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

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

通过应用强制转换,你已经告诉编译器this是OnInitListener的一个实例。这就是您不会得到编译错误的原因。然而,cast是在运行时检查的,当VM确定that这是而不是该接口的实例时,它会失败。因此下面的LogCat消息:

. lang。ClassCastException: com.example.texttospeech . mainactivity不能强制转换为android.speech. ttts .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. 
}

相关内容