随机变量的运行时错误

时间:2014-06-02 18:46:11

标签: java android eclipse android-layout android-intent

package com.manish;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity {
    ToggleButton pasTog;
    Button ChkCmd;
    EditText input;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        ChkCmd = (Button) findViewById(R.id.Bresults);
        pasTog = (ToggleButton) findViewById(R.id.tbPassword);
        input = (EditText) findViewById(R.id.etCommands);
        display = (TextView) findViewById(R.id.tvResults);
        pasTog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (pasTog.isChecked()) {
                    input.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                }
            }
        });

        ChkCmd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                String Check = input.getText().toString();
                display.setText(Check);
                if (Check.contentEquals("left")) {
                    display.setGravity(Gravity.LEFT);
                } else if (Check.contentEquals("center")) {
                    display.setGravity(Gravity.CENTER);
                } else if (Check.contentEquals("right")) {
                    display.setGravity(Gravity.RIGHT);
                } else if (Check.contentEquals("blue")) {
                    display.setTextColor(Color.BLUE);
                } else if (Check.contentEquals("WTF")) {
                    Random crazy = new Random();
                    display.setText("WTF!!!");
                    display.setTextSize(crazy.nextInt());
                    display.setText(Color.rgb(crazy.nextInt(),
                            crazy.nextInt(), crazy.nextInt()));
                    switch (crazy.nextInt(3)) {
                    case 0:
                        display.setGravity(Gravity.LEFT);
                        break;
                    case 1:
                        display.setGravity(Gravity.CENTER);
                        break;
                    case 2:
                        display.setGravity(Gravity.RIGHT);
                        break;
                    }
                } else {
                    display.setText("Invalid");
                    display.setGravity(Gravity.CENTER);
                    display.setTextColor(Color.BLACK);
                }

            }
        });
    }

}

logcat的:

06-02 23:57:08.352: E/AndroidRuntime(13738): FATAL EXCEPTION: main
06-02 23:57:08.352: E/AndroidRuntime(13738): android.content.res.Resources$NotFoundException: String resource ID #0xffca9612
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.content.res.Resources.getText(Resources.java:241)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.widget.TextView.setText(TextView.java:3943)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at com.manish.TextPlay$2.onClick(TextPlay.java:65)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.view.View.performClick(View.java:4231)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.view.View$PerformClick.run(View.java:17537)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.os.Handler.handleCallback(Handler.java:725)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.os.Looper.loop(Looper.java:158)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at android.app.ActivityThread.main(ActivityThread.java:5751)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at java.lang.reflect.Method.invokeNative(Native Method)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at java.lang.reflect.Method.invoke(Method.java:511)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
06-02 23:57:08.352: E/AndroidRuntime(13738):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您正在拨打错误的setText()版本:

  • TextView#setText(int resid)

这一行:

display.setText(Color.rgb(crazy.nextInt(), crazy.nextInt(), crazy.nextInt()));

应该是:

display.setText(Integer.toString(Color.rgb(crazy.nextInt(), crazy.nextInt(), crazy.nextInt())));