-1

我有一个包含景点列表的微调器。我想使用 switch case 语句来根据用户选择的吸引力来更改显示的图像。

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);

    attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
    attractionsSpinner.setAdapter(attractionsAdapter);
    attractionsSpinner.setOnItemSelectedListener(this);

}
    @Override
    public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
        // TODO Auto-generated method stub
        int pos = attractionsSpinner.getSelectedItemPosition();
        ImageView imageView = (ImageView) findViewById(R.id.imageViewAttraction);
        String[] information = getResources().getStringArray(R.array.attractions_information_collection);

        switch (position)
        {
        case 0:
            image = R.drawable.avenue_of_stars;
            imageView.setImageResource(image);

            break;
        case 1:
            image = R.drawable.disneyland_fountain;
            imageView.setImageResource(image);

            break;  
        }
    }

我收到一条错误消息,提示“无法将图像解析为变量”

4

2 回答 2

2

您没有为image. 在你开始开关之前声明它

int image = 0;
switch (position)
    {
    case 0:
        image = R.drawable.avenue_of_stars;
        imageView.setImageResource(R.drawable.avenue_of_stars;);

        break;
    case 1:
        image = R.drawable.disneyland_fountain;
        imageView.setImageResource(R.drawable.disneyland_fountain);

        break;  
    }
}

或者只使用值setImageResource()

  switch (position)
    {
    case 0:
        imageView.setImageResource(R.drawable.avenue_of_stars;);

        break;
    case 1:
        imageView.setImageResource(R.drawable.disneyland_fountain);

        break;  
    }
}

或简化它

int image = 0;
  switch (position)
    {
    case 0:
        image = R.drawable.avenue_of_stars;
        break;
    case 1:
        image = R.drawable.disneyland_fountain;
        break;  
    }
 imageView.setImageResource(R.drawable.disneyland_fountain);
}
于 2013-03-04T20:53:13.600 回答
1

错误消息告诉您您没有声明一个名为“image”的变量,因此当您使用该词时它不知道您要做什么。

将您的代码更改为如下所示:

private int image = -1; // <-- you have to declare a variable to be able to use it (in java).
private imageView; // <-- declare this ImageView up here too, while your at it.

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.attractions_layout);
    imageView = (ImageView) findViewById(R.id.imageViewAttraction); //Move findViewById() to here, calling it more than once is wasteful.
    ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);

    attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
    attractionsSpinner.setAdapter(attractionsAdapter);
    attractionsSpinner.setOnItemSelectedListener(this);

}
    @Override
    public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
        // TODO Auto-generated method stub
        int pos = attractionsSpinner.getSelectedItemPosition();
        String[] information = getResources().getStringArray(R.array.attractions_information_collection);
        switch (position)
        {
        case 0:
            image = R.drawable.avenue_of_stars;
            imageView.setImageResource(image);

            break;
        case 1:
            image = R.drawable.disneyland_fountain;
            imageView.setImageResource(image);

            break;  
        }
    }

您必须声明一个调用的 intimage才能将 id 存储到其中。

另请注意,findViewById()每次用户选择某些内容时调用以获取对您的图像视图的引用是浪费的,您应该在内部获取引用onCreate(),然后在每次后续调用时使用该引用setImageResource()

最后一个提示:您似乎缺乏对 Java 编程和语法的一些基础知识的掌握。我强烈建议您现在花点时间回去做一些工作,让您在深入了解一个复杂的 Android 项目之前更好地熟悉 Java 语言。这样做会让你的生活更轻松=)。

于 2013-03-04T20:52:13.057 回答