简体   繁体   中英

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:

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

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.

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:

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

That is because you are casting this , a reference to the activity, to a OnInitListener.

By applying the cast, you have told the compiler that the this is an instance of 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. Thus the following LogCat message:

java.lang.ClassCastException: com.example.texttospeechapp.MainActivity cannot be cast to 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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