开发者

Please help with this error when using separate class file in Android app

I'm having trouble trying to figure out how to use a function contained in a separate class file.

My separate class file is called EncounterGenerator.java. And I call it like this:

EncounterGenerator Encounter; testString = Encounter.EncounterGeneratorText();

So below is my main file called HelloAndroid.java:

package mot.HelloAndroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;


public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
TextView tvIntro;
TextView tvNavigator;
TextView tvAction;
EncounterGenerator Encounter ;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    //setup text views
    this.setCont开发者_高级运维entView(R.layout.main);

     tvIntro = (TextView)findViewById(R.id.tvIntro);
     tvNavigator = (TextView)findViewById(R.id.tvNavigator);
     tvAction = (TextView)findViewById(R.id.tvAction);


    //display game intro
    tvIntro.setText("Text Adventure");

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    tvIntro.setText("This is a really cool text adventure game!");

    //define navigation buttons
    Button btnNorth = (Button)findViewById(R.id.btnNorth);

    btnNorth.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tvIntro.setText("You go north");
            String testString = "nothing";
            try {
                testString = Encounter.EncounterGeneratorText();
            } catch (Exception e) {
                    e.printStackTrace();
                }
            testString = Encounter.EncounterGeneratorText();
            tvIntro.setText(testString);

        }
    });
  }
}

and this is my separate class file called EncounterGenerator.java:

package mot.HelloAndroid;

public class EncounterGenerator {

public String EncounterGeneratorText() {
    String encounterText = "test value";  
    return encounterText;
}
}

Everytime I run it, and then click the button that calls the function EncounterGeneratorText() I get this error in my AVP:

The application Hello Android has stopped unexpectedly...please try again

If anyone could help, I'd greatly appreciate it!


You need to instantiate Encounter:

EncounterGenerator Encounter = new EncounterGenerator();

As a side note, variables in Java usually starts with a lower case letter.


your method is not a static method. you need to instanciate your class in order to be able to call the method EncounterGeneratorText() .

Second point : try to use a CountDownTimer(long delay, long unit); not

Thread.sleep(long milliseconds);

Note : methods always start with lowerCase :) .

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜