1

在 Android 应用程序中,我想为第一个和第二个按钮分配不同的标签(在你按下任何一个按钮之后)。我的代码如下。LogCat 向我展示了它执行一次内部 for 循环,然后关闭 VM 并给出消息"threadid=1: thread exists with uncaught exception (group=0x409961f8)"。谢谢你的帮助。

int marked = 0;
    int i = 0;
    int a = i + 1;

    for ( i = 0; i < priorities.size(); i++ ) 
    {
            Log.d(TAG, "Setting button one tag: " + i );
            Log.d(TAG, "blablabla rank2 " + priorities.get(i).rank);
            button_one.setTag(i);
            button_one.setText(priorities.get(i).name);

            for (a = i + 1; a <= priorities.size(); a++)
            {
            Log.d(TAG, "Setting whilee: " + i );
            Log.d(TAG, "blablabla while " + priorities.get(i).rank);
            button_two.setTag(a);
            button_two.setText(priorities.get(a).name);

            }       
    }
4

1 回答 1

1

我不能确定,但​​我不认为你真的想在这种情况下使用嵌套的 for 循环。您可能想要这个:

int marked = 0;
int i = 0;
int a = i + 1;

for ( i = 0; i < priorities.size(); i++ ) 
{
        Log.d(TAG, "Setting button one tag: " + i );
        Log.d(TAG, "blablabla rank2 " + priorities.get(i).rank);
        button_one.setTag(i);
        button_one.setText(priorities.get(i).name);

        Log.d(TAG, "Setting whilee: " + i );
        Log.d(TAG, "blablabla while " + priorities.get(i).rank);
        button_two.setTag(i);
        button_two.setText(priorities.get(i).name);
}

如果您确实需要嵌套循环,那么您与循环计数器不一致(外循环索引为零,但内循环索引为一,并且您正在对相同类型的对象进行操作。在这种情况下你想要这个:

int marked = 0;
int i = 0;
int a = i + 1;

for ( i = 0; i < priorities.size(); i++ ) 
{
        Log.d(TAG, "Setting button one tag: " + i );
        Log.d(TAG, "blablabla rank2 " + priorities.get(i).rank);
        button_one.setTag(i);
        button_one.setText(priorities.get(i).name);

        for (a = i; a < priorities.size(); a++)
        {
            Log.d(TAG, "Setting whilee: " + i );
            Log.d(TAG, "blablabla while " + priorities.get(i).rank);
            button_two.setTag(a);
            button_two.setText(priorities.get(a).name);
        }       
}

虽然我不是 100% 确定。如果没有对所有变量的定义(什么是 button_one 和 button_two?什么是优先级?),就很难分辨。

于 2012-08-14T19:26:55.327 回答