简体   繁体   English

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

[英]When running my program on eclipse android real device i'm getting many errors in the LogCat what all this errors and how to fix them?

On my smartphone I saw a message unfortunately the app has stopped. 不幸的是,在我的智能手机上,我看到一条消息,提示该应用已停止。

Then I saw this errors in the 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

And this is my code in MainActivity.java 这是我在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);
    }
}

I'm not getting any errors in my code. 我的代码没有任何错误。 9 warnings but no errors. 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);
                    }
        }
    }
}

==== Problem ==== ==== 问题 ====

You are getting a ClassCastException on this line of MainActivity: 您在MainActivity的这一行上收到ClassCastException:

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

That is because you are casting this , a reference to the activity, to a OnInitListener. 那是因为您正在将this (对活动的引用)转换为OnInitListener。

By applying the cast, you have told the compiler that the this is an instance of OnInitListener. 通过应用强制转换,您已经告诉编译器this是OnInitListener的实例。 That is why you do not get a compilation error. 这就是为什么您没有得到编译错误的原因。 The cast is however checked at runtime, and it fails when the VM determines that this is not an instance of that interface. 但是,将在运行时检查强制类型转换,当VM确定that 不是该接口的实例时,它将失败。 Thus the following LogCat message: 因此,出现以下LogCat消息:

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

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

To fix this you must implement that interface in your activity as follows 要解决此问题,您必须在活动中实现该接口,如下所示

public class MainActivity extends ActionBarActivity implements OnInitListener {

and then you will need to implement the one OnInitListner method in your activity: 然后您将需要在活动中实现一个OnInitListner方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM