开发者

Calling a method from another class in TextToSpeech API

I am using the TextToSpeech API and I want to seperate some logic into another class.

In the separate class I have put the following method:

public static void sayHello() {
// Select a random hello.
int helloLength = SoundGameScore.Questions.length;
String hello = SoundGameScore.Questions[currentHelloIndex];
currentHelloIndex = (currentHelloIndex + 1) % helloLength;
mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, // Drop all pending entries
                                            // in the playback queue.
        null);

I have then created开发者_C百科 a variable in the main class: static mainclass object;

Within a button in the main class I call the method through this object by using:

object.sayHello();

I am quite new to android, and I know I am doing something wrong as this gives me a process closed error in the emulator. This also shows a nullexception error in logcat. Please help me, thanks.


I think you are getting a NullPointerException because the reference object is null. You would need to initialise the object in order to call an instance method on it.

However since sayHello() is a static method, you do not need to create an instance of the class in order to call the method. Just use mainclass.sayHello().

Your question and code suggests to me that you do not have much experience with Java. Perhaps you should do some tutorials to brush up on your Java coding before jumping into Android development. For example, Java convention is for class names to be capitalised (MainClass) and for references to have meaningful names (i.e. not things like object).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜