Android列表查看图像视图错误?

时间:2016-09-09 09:56:22

标签: android imageview

在我的Android应用程序列表视图中有一个图像。 imageview在另一个布局上。我想通过代码将该图像更改为另一个图像。当我用代码运行程序时出现错误..'

任何人都可以帮我找到错误谢谢你./

我试过..

  Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
                down.setImageBitmap(bImag);

,这也是

myImgView.setBackgroundResource(R.drawable.monkey);

我的xml代码..(我在imageview上使用了Onclick)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/Itemname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:text="asdas"
        android:layout_weight="0.9"
        android:paddingTop="5dp"/>

    <ImageView
        android:id="@+id/icon"
        android:onClick="download"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:gravity="right"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/download" />

</LinearLayout>

Java代码..

 public void download(View view) {
        String value = getIntent().getExtras().getString("id");
        if (value.equals("Pretham (2016)")) {
            Intent i=new Intent(Song_List.this, MyService.class);
            startService(i);
            Toast.makeText(Song_List.this, "Downloading..........", Toast.LENGTH_SHORT).show();
            Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
            down.setImageBitmap(bImag);
        }else{
            Toast.makeText(Song_List.this, "Downloaded Song.....", Toast.LENGTH_SHORT).show();
        }

我的logcat是..

Process: com.example.jithin.myapplication, PID: 1539
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:5201)
    at android.view.View$PerformClick.run(View.java:21163)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)

1 个答案:

答案 0 :(得分:0)

imageView有:

android:onClick="download"

仅当布局来自Activity setContentView()而不是listview或Fragment

时才有效

您需要添加适配器:

 ImageView img = findViewById(R.id.download);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //CODE on click
        }
    });

并从xml中删除onclick