Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么随机数会使我的Android应用程序崩溃?_Java_Android_Random - Fatal编程技术网

Java 为什么随机数会使我的Android应用程序崩溃?

Java 为什么随机数会使我的Android应用程序崩溃?,java,android,random,Java,Android,Random,我试图显示一个随机的事实,并在每次事实改变时改变背景颜色 我在MainActivity上使用一个随机数,但只要按下按钮生成并显示该随机数,应用程序就会崩溃 以下是GitHub上项目的链接: 以下是logCat中显示的错误: 06-09 16:36:27.125 14551-14551/com.howtoevery.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.howtoevery.funfacts, PI

我试图显示一个随机的事实,并在每次事实改变时改变背景颜色

我在
MainActivity
上使用一个随机数,但只要按下按钮生成并显示该随机数,应用程序就会崩溃

以下是GitHub上项目的链接:

以下是logCat中显示的错误:

 06-09 16:36:27.125  14551-14551/com.howtoevery.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.howtoevery.funfacts, PID: 14551
    android.content.res.Resources$NotFoundException: String resource ID #0x0
            at android.content.res.Resources.getText(Resources.java:275)
            at android.widget.TextView.setText(TextView.java:4261)
            at com.howtoevery.funfacts.MainActivity$1.onClick(MainActivity.java:56)
            at android.view.View.performClick(View.java:4763)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
下面是
MainActivity.java
代码:

package com.howtoevery.funfacts;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Creating the random
        final Random rand = new Random();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView factLabel;
        final Button showFactButton;

        factLabel = (TextView) findViewById(R.id.fact);
        final ViewGroup backGround = (RelativeLayout) findViewById(R.id.mainBackground);
        showFactButton = (Button) findViewById(R.id.factButton);

        View.OnClickListener myListener = new View.OnClickListener() {

            int randomNumber;

            @Override
            public void onClick(View view) {
                randomNumber = rand.nextInt(1); // randomNumber will be 0 or 1

                switch (randomNumber) {
                    case 0: { // in case it is 0, show fact number 1 with green color
                        factLabel.setText(R.string.fact1);
                        backGround.setBackgroundColor(getResources().getColor(R.color.android_green));
                        showFactButton.setTextColor(getResources().getColor(R.color.android_green));
                    } break;

                    case 1: { // in case it is 1, show fact number 2 with orange color
                        factLabel.setText(R.string.fact2);
                        backGround.setBackgroundColor(getResources().getColor(R.color.orangish));
                        showFactButton.setTextColor(getResources().getColor(R.color.orangish));
                    } break;
                }

                factLabel.setText(randomNumber); // show the number generated with randomNumber, ONLY FOR DEBUG PURPOSES

            }
        };

        showFactButton.setOnClickListener(myListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
我对应用程序的期望是,它将显示不同的事实并更改颜色(现在,为了调试,我希望看到文本更改为生成的随机数,并且颜色已更改)


为什么它不能像我期望的那样工作?

检查所有要用作
R.color.
R.string.
的资源。根据异常所在行的编号(MainActivity.java:56,我认为),您可以找到应用程序中不存在的资源,然后转到特定文件(colors.xml、strings.xml等)并添加它。

问题在于这一行:
factLabel.setText(randomNumber)。您想设置表示您的
randmNumber
的字符串。使用

factLabel.setText(String.valueOf(randomNumber)); 

您使用的
setText
版本查找id为您提供的int的字符串。如果找不到,它将抛出该异常

根据错误,您正在引用一个不存在的资源,它与随机数无关。@llogiq,不存在的资源是什么?
randomNumber
变量确实存在。@IshayFrenkel:Resource,而不是variable.@T.J.Crowder,我不明白,因为catLog中提到的
MainActivity
中的第56行是
factLabel.setText(randomNumber)我使用的所有资源都存在。谢谢!我不知道我需要把那个
randomNumber
作为一个字符串。(我会尽快将此标记为答案)有人能告诉我我做错了什么,以至于我在这个问题上投了4张反对票吗?这与问题无关。。
MainActivity
中的第56行是
factLabel.setText(随机数)并且所有资源都没有问题或丢失,但感谢您尝试帮助我:)