0

Brief explanation of application thus far:

the user clicks on the start button in MainActivity.java and will be redirected to a new activity (FirstWord.java) with a TextToSpeech instance. It might be possible that issues with FirstWord.java might cause the crash [?] .

MainActivity.java [relevant portion]

public void startClicked(View view){
        Intent i = new Intent( MainActivity.this, FirstWord.class );
        MainActivity.this.startActivity(i);

    }

activity_main.XML, I have done the android:onClick="startClicked" attribute to a button.

FirstWord.java

package com.example.learnwords;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class FirstWord extends Activity implements OnClickListener, OnInitListener{

     private TextToSpeech myTTS;
     private int MY_DATA_CHECK_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button proButton = (Button)findViewById(R.id.proButton);
        proButton.setOnClickListener(this);     

        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
    }

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

    public void proClicked(View view){
        TextView textView = (TextView)findViewById(R.id.textView1);
        String word = textView.getText().toString();
        speakWords(word);
    }
    private void speakWords(String speech) {
        //speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                //the user has the necessary data - create the TTS
            myTTS = new TextToSpeech(this, this);
            }
            else {
                    //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }
        //setup TTS
    public void onInit(int initStatus) {
            //check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        }
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }
    }

        @Override
        public void onClick(View v) {

        }
}

Full Logcat: There is a NullPointer Exception and a RunPointer Exception

09-18 21:15:45.094: D/ActivityThread(4730): setTargetHeapUtilization:0.25
09-18 21:15:45.094: D/ActivityThread(4730): setTargetHeapIdealFree:8388608
09-18 21:15:45.104: D/ActivityThread(4730): setTargetHeapConcurrentStart:2097152
09-18 21:15:45.515: I/Adreno200-EGLSUB(4730): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-18 21:15:45.525: E/(4730): <s3dReadConfigFile:75>: Can't open file for reading
09-18 21:15:45.535: E/(4730): <s3dReadConfigFile:75>: Can't open file for reading
09-18 21:15:48.408: W/dalvikvm(4730): threadid=1: thread exiting with uncaught exception (group=0x4196f438)
09-18 21:15:48.408: E/AndroidRuntime(4730): FATAL EXCEPTION: main
09-18 21:15:48.408: E/AndroidRuntime(4730): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learnwords/com.example.learnwords.FirstWord}: java.lang.NullPointerException
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.os.Looper.loop(Looper.java:137)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread.main(ActivityThread.java:4950)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at java.lang.reflect.Method.invoke(Method.java:511)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at dalvik.system.NativeStart.main(Native Method)
09-18 21:15:48.408: E/AndroidRuntime(4730): Caused by: java.lang.NullPointerException
09-18 21:15:48.408: E/AndroidRuntime(4730):     at com.example.learnwords.FirstWord.onCreate(FirstWord.java:27)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.Activity.performCreate(Activity.java:5179)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-18 21:15:48.408: E/AndroidRuntime(4730):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-18 21:15:48.408: E/AndroidRuntime(4730):     ... 11 more

Any Ideas? Thanks

1 Answer 1

2

This line: setContentView(R.layout.activity_main);

It seems like you're loading the layout for the main activity and not the one for FirstWorld. Use the layout file for FirstWorld instead. You are getting the NullPointer because the following code after setContentView initializes widgets/forms that are NOT found in the activity_main layout file.

Use the layout file for FirstWorld instead, it would go something like: setContentView(R.layout.activity_first_world); or whatever the name of your FirstWorld layout file is.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.