简体   繁体   English

在Android Studio中使用Spinner添加图像

[英]Add Images using Spinner in Android Studio

I'm trying to load image in MainActivity based on selected Spinner Element. 我正在尝试根据选定的Spinner Element在MainActivity中加载图像。

So if my spinner consist two channel names in layout, and these names are stored in array channels = ["skysports", "premierlive"] 因此,如果我的微调器在布局中包含两个通道名称,并且这些名称存储在数组通道= [“ skysports”,“ premierlive”]中

I have defined my ImageView: 我已经定义了ImageView:

ImageView logo = (ImageView) findViewById(R.id.logo);

While creating Spinner I'm using this channels array and from there I'm calling 创建Spinner时,我使用了这个channel数组,然后从那里调用

getLogo(channels[i]); 

where channels[i] is current selected Spinner element. 其中channel [i]是当前选定的Spinner元素。

Finally, I'm trying to implement this image switch with switch-case inside getLogo: 最后,我尝试使用getLogo中的switch-case实现此图像开关:

public void getLogo(String channel){
        switch(channel){
            case ("skysports"):
                logo.setImageResource(R.drawable.skysports);
                break;
            case ("premierlive"):
                logo.setImageResource(R.drawable.premierlive);
                break;
        }
    }

All this gives me 这一切都给我

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.ImageView.setImageResource(int)”

What shall I do? 我该怎么办? Tnx 特纳克斯

EDIT: 编辑:

activity_main: activity_main:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

     <Spinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnerMain"
            android:paddingLeft="30dp"
            android:paddingRight="10dp"
            android:paddingTop="2dp"
            android:paddingBottom="2dp"
            android:clickable="true"
            android:touchscreenBlocksFocus="true"
            android:layout_alignParentTop="true"
            android:visibility="visible"
            android:textSize="@android:dimen/app_icon_size"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp" />

</RelativeLayout>

and MainActivity: 和MainActivity:

   public class MainActivity extends ActionBarActivity implements View.OnClickListener {
        String[] channels;
        String channel;
        Spinner sp;
        ImageView logo;


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView logo = (ImageView) findViewById(R.id.logo);
         sp = (Spinner) findViewById(R.id.spinnerMain);
        addSpinner();

 public void addSpinner(){
        channels = getChannels();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, channels);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int index = parent.getSelectedItemPosition();
                getLogo(channels[index]);    
               }
         });
   }    
}

} }

I ser in your activity a private field logo and in the onCreate method a local variable called logo which is the variable initialized. 我在您的活动中提供一个私有字段徽标,在onCreate方法中提供一个名为logo的局部变量,该变量已初始化。 In onCreate method you need to initialize the private field to be used in other methods of your class, rather than have 在onCreate方法中,您需要初始化私有字段以在您的类的其他方法中使用,而不是具有

ImageView logo = (ImageView) ...

You should have 你应该有

logo = (ImaView) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM