【问题标题】:Android: Random Word GeneratorAndroid:随机词生成器
【发布时间】:2013-12-02 05:21:17
【问题描述】:

到目前为止,我有这个,但我不能很好,有什么问题,对不起这个愚蠢的问题,我对这一切都是新手。 我还发布了它给我的错误以供进一步参考

package com.example.randomword;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends Activity {

String[] quotes = new String[] {"q1", "q2", "q3"};
String randomQuote = quotes[(int) (Math.random() * quotes.length)];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Make a new text view passing Activity object
TextView quote = new TextView(R.id.quote);
//Set a text into view

quote.setText(quotes);             <--- Error here "The method setText(CharSequence) in the type TextView is not applicable for the arguments (String[])"
//set the view into activity view container
setContentView(quote);

}
}

【问题讨论】:

    标签: java android eclipse random textview


    【解决方案1】:

    不要在TextView中设置字符串数组,除了只设置字符串如下你不能在TextView中设置String array

    quote.setText(randomQuote);  
    

    【讨论】:

      【解决方案2】:

      Quotes 是一个字符串数组,而不仅仅是一个字符串。你想用

      quote.setText(randomQuote);
      

      如果您确实想将值设置为字符串数组,例如用新行展开,您可以这样做:

      String quotesString = "";
      for(String s : quotes) 
          quoteString += s + "\n";
      quote.setText(quotesString);
      

      【讨论】:

      • 现在我在 TextView 上遇到错误 quote = new TextView(R.id.quote); “TextView(int) 未定义”
      • 没问题。对于新错误,请尝试 TextView quote = new TextView(this);
      【解决方案3】:

      您将字符串数组传递给TextViewsetText 方法。这就是问题所在。
      quotes 声明为:

      String[] 引号 = new String[] {"q1", "q2", "q3"};

      然后你写道:

      quote.setText(quotes);

      您只需将一个String 传递给setText
      要么:

      quote.setText(randomQuote);

      quote.setText(quotes[i]); // 其中i是一个整数,表示quotes中元素的索引

      【讨论】:

        猜你喜欢
        • 2017-09-03
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多