【问题标题】:Problem:String array set on TextView问题:TextView上设置的字符串数组
【发布时间】:2011-09-08 06:22:44
【问题描述】:

我想在单击下一个按钮时更改文本视图中的文本。我试过这段代码,但找不到正确的方向。请你修一下好吗?

public class StringTestActivity extends ListActivity {
TextView t;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t=(TextView)findViewById(R.id.text);

    Resources res = this.getResources();
    final String[] capital = res.getStringArray(R.array.Cap);

       //ArrayAdapter<String> n=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,capital);
       //setListAdapter(n);
    Button next=(Button)findViewById(R.id.btnNext);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(count<capital.length-1)
            t.setText(capital[count]);
            count++;
        }

    });

} }

编辑:我遇到了这个错误信息。

再次编辑: main.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <TextView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:hint="text" />

   <Button android:text="Next" android:id="@+id/btnNext"
 android:layout_width="wrap_content" android:layout_height="wrap_content">   </Button>        

</LinearLayout>

res/string/array.xml

  <?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string-array name="Cap">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <item>D</item>
  </string-array>
 </resources>

【问题讨论】:

  • 你怎么了?你能详细说明你遇到了什么问题吗?
  • 当我运行这个项目时,上面的信息是显示的(对不起.....)。我的代码有什么问题。
  • 向我们提供您在 logcat 中收到的错误消息。不是他的形象。
  • java.lang.RuntimeException:无法启动活动 ComponentInfo{a.b/a.b.StringTestActivity}:java.lang.RuntimeException:您的内容必须有一个 ID 属性为 'android.R.id.list 的 ListView '
  • 在下面查看我的回答。希望对您有所帮助。

标签: android arrays


【解决方案1】:
Button btn1;
 String countires[];
 int i=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prob2);

    btn1 = (Button) findViewById(R.id.prob2_btn1);

    countires = getResources().getStringArray(R.array.country);

    for (String string : countires)
    {
        Log.i("--: VALUE :--","string = "+string);
    }

    btn1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            String  country  = countires[i];
            btn1.setText(country);
            i++;
            if(i==countires.length)
                i=0;
        }
    });
}

【讨论】:

  • @AB,谢谢,您也可以帮助解决这个问题。
【解决方案2】:

如果您不使用 ListView,则实施 Activity 而不是 ListActivity。所以做这个改变

公共类 StringTestActivity 扩展 Activity {

另外导入android.app.Activity

【讨论】:

  • ..请仔细查看我编辑的问题。希望你能理解我的问题。
  • 在线出错>>> t=(TextView)findViewById(R.id.list);在列表上。
  • 能否请您给出这个问题背后的一些想法:stackoverflow.com/questions/7332112/…
【解决方案3】:
@Override
        public void onClick(View v) {
            t.setText(capital[count]);
            count++;
            count=count%capital.length;
        }

【讨论】:

  • 使用 t=(TextView)findViewById(R.id.list);
【解决方案4】:

您的代码看起来不错,“无法找到正确的方向”是什么意思?会不会因为异常而崩溃,代码永远不会改变吗?

顺便说一句,考虑到你的按钮是 next 调用的,你可能想这样重写它:

    Override
    public void onClick(View v) {
        count++;
        if( count < capital.length ) // no - 1
        {
            t.setText(capital[count]);
        } else {
            /* perhaps v.setEnabled(false); ? */
        }
    }

【讨论】:

  • 我已经尝试过这个 capital.length 了,但是不行。告诉我更多。
  • 您确定强制关闭来自该代码吗?发布日志而不只是一张图片怎么样?
  • 09-08 12:25:21.420: ERROR/AndroidRuntime(13400): java.lang.RuntimeException: Unable to start activity ComponentInfo{a.b/a.b.StringTestActivity}: java.lang.RuntimeException: 你的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
  • 嗯,这与您发布的代码无关。您得到的例外是因为“您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”。检查您的布局 (R.layout.main) 并确保有一个名为 @android:id/list 的 ListView 项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多