由随机变量引起的运行时错误

时间:2014-06-05 18:19:03

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

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(75));
                    display.setText(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
                            crazy.nextInt(265)));
                    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);
                }

            }
        });
    }

}

此代码产生以下错误

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

1 个答案:

答案 0 :(得分:1)

public static int rgb (int red, int green, int blue)

Added in API level 1
Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.

Parameters
red Red component [0..255] of the color
green   Green component [0..255] of the color
blue    Blue component [0..255] of the color

如您所见,返回类型为int。

  

android.content.res.Resources $ NotFoundException:字符串资源ID#0xffca961

更改此

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

display.setText(String.valueOf(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),crazy.nextInt(265))));

setText(int)查找带有提及ID的资源,如果找不到,则会ResourceNotFoundExcpetion

您需要的是setText(CharacterSequence)

编辑:

正如Chris Stratton建议您使用setText两次以上来进行显示。每次将新数据设置为Textview

如果您想使用append代替setText附加数据。

或者您可能打算将颜色设置为textview,这可能是您想要的。