0

迄今为止的应用简要说明:

用户单击 MainActivity.java 中的开始按钮,将被重定向到具有 TextToSpeech 实例的新活动 (FirstWord.java)。FirstWord.java 的问题可能会导致崩溃 [?] 。

MainActivity.java [相关部分]

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

    }

activity_main.XML,我已经完成android:onClick="startClicked"了按钮的属性。

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) {

        }
}

完整的Logcat: 有一个NullPointer 异常和一个RunPointer 异常

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

有任何想法吗?谢谢

4

1 回答 1

2

这一行:setContentView(R.layout.activity_main);

似乎您正在加载主要活动的布局,而不是 FirstWorld 的布局。请改用 FirstWorld 的布局文件。您获得 NullPointer 是因为以下代码在setContentView初始化activity_main布局文件中未找到的小部件/表单之后。

改用 FirstWorld 的布局文件,它会变成: setContentView(R.layout.activity_first_world);或者您的 FirstWorld 布局文件的名称。

于 2013-09-19T01:32:54.197 回答