Toast Message不工作android app eclipse

时间:2014-04-14 06:02:19

标签: java android if-statement crash-reports

Toast消息仍然无法正常工作,运行正常,但是当没有输入数字并且点击按钮时,应用程序崩溃并且说不幸的是已停止工作。只是想让我的应用更具防撞功能,让用户享受更多

代码:

公共类MainActivity扩展了Activity {

public static final String TAG = MainActivity.class.getCanonicalName();
TextView textOne;
TextView guessText;
EditText userGuess;
Button pushMe;
MediaPlayer applause;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    varSet();






    pushMe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Log.w(TAG, "Button Clicked");
            String randText = "";
            // TODO Auto-generated method stub
            Random randGen = new Random();
            int rando = randGen.nextInt(10) +1;

             if((guessText.getText().toString()).length() == 0)
                {
                    Toast.makeText(getApplicationContext(), "no number entered", Toast.LENGTH_SHORT).show();
                }
                else{

            int userNumber = Integer.parseInt(userGuess.getText().toString());


            if ( userNumber < 1 || userNumber > 10 ) {
                guessText.setText("Please guess 1 - 10");
            } else if ( userNumber == rando) {
                guessText.setText("You Got It Right!");
                if (applause.isPlaying()) {
                    applause.seekTo(0);
                } else {
                  applause.start();
                }
            } else {
                guessText.setText("Not quite, guess again!");
            }

            randText = Integer.toString(rando);
            textOne.setText(randText);

            userGuess.setText("");
        }
        }});

}


private void varSet() {
      textOne = (TextView) findViewById(R.id.textView1);
      guessText = (TextView) findViewById(R.id.textView2);
      userGuess = (EditText) findViewById(R.id.editText1);
      pushMe = (Button) findViewById(R.id.button1);
      applause = MediaPlayer.create(MainActivity.this, R.raw.applause);
}

谢谢你,任何人都会很棒!!!

3 个答案:

答案 0 :(得分:2)

如果您想添加Toast消息,请查看Android开发人员指南http://developer.android.com/guide/topics/ui/notifiers/toasts.html(一个适合您所有Android开发相关问题的好地方)

这一切都从这样的事情开始:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

答案 1 :(得分:0)

在你的onClick方法中你可以这样写:

    OnClickListener buttonClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            if((guessText.getText().toString()).length() == 0)
            {
                Toast.makeText(getApplicationContext(), "no number entered", Toast.LENGTH_SHORT).show();
            }
            else{
                int userNumber = Integer.parseInt(userGuess.getText().toString());

                                     if ( userNumber < 1 || userNumber > 10 ) {
            guessText.setText("Please guess 1 - 10");
        } else if ( userNumber == rando) {
            guessText.setText("You Got It Right!");
            if (applause.isPlaying()) {
                applause.seekTo(0);
            } else {
              applause.start();
            }
        } else {
            guessText.setText("Not quite, guess again!");
        }

        randText = Integer.toString(rando);
        textOne.setText(randText);

        userGuess.setText("");
            }
        }
    };

    pushMe.setOnClickListener(buttonClickListener);

答案 2 :(得分:0)

尝试以下代码: -

showToast(this, "your message");

public static void showToast(Activity activity, String msg)
{
    try
    {
        Toast toast = Toast.makeText(activity, msg, Toast.LENGTH_LONG);
        toast.getView().setBackgroundColor(activity.getResources().getColor(R.color.purple_dark));
        toast.show();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}