简体   繁体   English

Toast 消息不起作用 android 应用程序 eclipse

[英]Toast Message Not Working android app eclipse

Toast message still not working any help please, runs fine but when no number entered and button is clicked app crashes and says unfortunately has stopped working. Toast 消息仍然没有任何帮助,运行正常,但是当没有输入数字并单击按钮时,应用程序崩溃并说不幸的是已停止工作。 Just wanted to make my app more crash proof for users to enjoy more只是想让我的应用程序更防崩溃,让用户享受更多

code:代码:

public class MainActivity extends Activity {公共类 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);
}

Thank you any would be GREAT!!!谢谢你会很棒!!!

If you want to add a toast message, have a look at the Android developers guide http://developer.android.com/guide/topics/ui/notifiers/toasts.html (a good place to start for all your Android development related questions)如果您想添加 Toast 消息,请查看 Android 开发者指南http://developer.android.com/guide/topics/ui/notifiers/toasts.html (这是开始所有 Android 开发相关的好地方问题)

Which all starts with something like this:这一切都始于这样的事情:

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

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

In your onClick method you can write this:在您的 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);

try below code :-试试下面的代码:-

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM