2015-06-09 149 views
1

我試圖顯示一個隨機事實,並在事實發生變化時更改背景顏色。爲什麼隨機數會崩潰我的Android應用程序?

我使用MainActivity上的一個隨機數做它,但只要我按下按鈕來生成並顯示該應用程序崩潰的隨機數。

這裏是鏈接到該項目在GitHub上: https://github.com/ishay1999/FunFacts

這裏是在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); 
    } 
} 

我對應用程序的期望是,它會顯示不同的事實並更改顏色(現在,爲了調試,我希望看到文本更改s到生成的隨機數並且顏色已經改變)

爲什麼它不能像我期望的那樣工作?

+4

根據該錯誤,您引用的是不存在的資源,並且它與隨機數無關。 – llogiq

+0

@llogiq,什麼是不存在的資源? 'randomNumber'變量** do **存在。 –

+0

@IshayFrenkel:*資源*,不可變。 –

回答

3

問題在這裏是這條線:factLabel.setText(randomNumber);。你想設置代表你的randmNumber的字符串。使用

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

setText版本使用的是查找與ID的字符串的詮釋您提供的。如果找不到,則會引發該異常

+0

謝謝!我不知道我需要將'randomNumber'作爲一個字符串。 (我會盡快將其標記爲答案) 有人可以告訴我我做錯了什麼,我得到了4個關於這個問題的提示嗎? –

+0

我不知道。抱歉 – Blackbelt

0

檢查您想要使用的所有資源R.color.<something>R.string.<something>。根據你的例外行的數量(MainActivity.java:56我認爲),你可以找到你的應用程序中沒有確定哪個資源,並轉到某個文件(colors.xml,strings.xml等)並添加它

+0

這與問題無關..'MainActivity'中的第56行是'factLabel.setText(randomNumber);'並且沒有資源有問題或缺失,但是謝謝你試圖幫助我:) –

相關問題