新活动因运行时异常和空指针异常而崩溃

时间:2013-09-19 01:31:12

标签: java android android-activity

到目前为止对申请的简要说明:

用户单击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

任何想法? 感谢

1 个答案:

答案 0 :(得分:2)

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

看起来你正在加载主要活动的布局,而不是FirstWorld的布局。 使用FirstWorld的布局文件。您正在获取NullPointer,因为setContentView之后的以下代码初始化了activity_main布局文件中找不到的窗口小部件/窗体。

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