Setimageresource android ошибка

This is the first canvas:

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

  <ScrollView
          android:id="@+id/scrollView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

    <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

      <TableRow
              android:id="@+id/TableRow01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img1"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:src="@drawable/girbaud1front" />

      </TableRow>

      <TableRow
              android:id="@+id/tableRow2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img2"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud4front" />

      </TableRow>

      <TableRow
              android:id="@+id/tableRow4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img3"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud2front" />

      </TableRow>

      <TableRow
              android:id="@+id/TableRow6"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/TableRow7"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img4"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud3front" />

      </TableRow>

      <TableRow
              android:id="@+id/TableRow02"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

    </TableLayout>
  </ScrollView>

</RelativeLayout> 

The second canvas that image change will happen is this file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

  <ImageView
          android:id="@+id/ImageView01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginLeft="10dp"
          android:scaleType="fitXY"
          android:src="@drawable/girbaud1front" />

  <TextView
          android:id="@+id/txt1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_marginBottom="62dp"
          android:layout_marginLeft="48dp"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="#000000" />

</RelativeLayout>

Java file:

package com.example.flip3d;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


public class Cards extends Activity {

  ImageView img1;
  ImageView img2;
  ImageView image;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cards);

    img1 =(ImageView) findViewById(R.id.img1);
    img2 =(ImageView) findViewById(R.id.img2);
    image =(ImageView) findViewById(R.id.ImageView01);

    img1.setOnClickListener(new View.OnClickListener() {

      public void onClick(View arg0) {

        image.setImageResource(R.drawable.myfirstimage);
        Intent startyou = new Intent("com.example.flip3d.FLIP3D");
        startActivity(startyou);
      }
    });

    img2.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        image.setImageResource(R.drawable.may2ndimage);

        Toast.makeText(Cards.this, "Card Selected", Toast.LENGTH_SHORT).show();

        Intent startyou = new Intent("com.example.flip3d.FLIP3D");
        startActivity(startyou);
      }
    });

  }

}

My logcat message.

When I run this code on my phone. An error occurred and when I check the logcat it says: that I’m having error with setImageResource();.

What should I do? I’m a beginner in Android programming.

Thanks in advance.

I’m trying to change the source of my ImageButton, but for some reason I keep getting this error message: Cannot resolve method ‘setImageResource’ (android.graphics.drawable.Drawable).

Here’s the code im trying to use:

<ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:id="@+id/cuenta_1"
        android:text="cuenta1"
        android:background="@mipmap/ic_fondo"
        android:layout_toLeftOf="@id/agregarCuenta"/>

And in the mainActivity:

Drawable logo = new BitmapDrawable(bitmap);
                        findViewById(R.id.cuenta_2).setImageResource(logo);

Any idea on how I can fix this?

Phantômaxx's user avatar

Phantômaxx

37.9k21 gold badges83 silver badges114 bronze badges

asked Jun 24, 2015 at 17:50

Andres's user avatar

0

I can see 2 errors

Change

findViewById(R.id.cuenta_2).setImageResource(logo);

to

// Create and cast an object of type ImageButton, with the proper id
ImageButton imb = (ImageButton) findViewById(R.id.cuenta_1); // you were trying to find cuenta_2, which is not in the layout
imb.setImageResource(R.drawables.some_image); // setImageResource takes an int, not a drawable.

For your reference: http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29

answered Jun 24, 2015 at 17:55

Phantômaxx's user avatar

PhantômaxxPhantômaxx

37.9k21 gold badges83 silver badges114 bronze badges

2

Всем привет,столкнулся с такой проблемкой,не могу понять почему setImageResource,крашит приложение делаю так

ImageView simpleImageView = (ImageView) findViewById(R.id.ColMemberID);
simpleImageView.setImageResource(R.mipmap.ic_launcher);

А в консоле вот так

W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ovorobyov.radiomaterial, PID: 5141
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ovorobyov.radiomaterial/ovorobyov.radiomaterial.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5345)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                      at ovorobyov.radiomaterial.MainActivity.onCreate(MainActivity.java:80)
                      at android.app.Activity.performCreate(Activity.java:6012)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5345) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 
I/Process: Sending signal. PID: 5141 SIG: 9
Application terminated.

this is the first canvas.


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

                <ScrollView
                    android:id="@+id/scrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >



                    <TableLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TableRow
                            android:id="@+id/TableRow01"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/TextView03"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" "
                                android:textAppearance="?android:attr/textAppearanceLarge" />
                        </TableRow>


                        <TableRow
                            android:id="@+id/tableRow1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >





                            <ImageView
                                android:id="@+id/img1"
                                android:layout_width="wrap_content"
                                android:layout_height="150dp"
                                android:src="@drawable/girbaud1front" />

                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/TextView01"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" "
                                android:textAppearance="?android:attr/textAppearanceLarge" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >




                            <ImageView
                                android:id="@+id/img2"
                                android:layout_width="320dp"
                                android:layout_height="150dp"
                                android:src="@drawable/girbaud4front" />

                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" "
                                android:textAppearance="?android:attr/textAppearanceLarge" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >




                            <ImageView
                                android:id="@+id/img3"
                                android:layout_width="320dp"
                                android:layout_height="150dp"
                                android:src="@drawable/girbaud2front" />

                        </TableRow>


                        <TableRow
                            android:id="@+id/TableRow6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/TextView02"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" "
                                android:textAppearance="?android:attr/textAppearanceLarge" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/TableRow7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >




                            <ImageView
                                android:id="@+id/img4"
                                android:layout_width="320dp"
                                android:layout_height="150dp"
                                android:src="@drawable/girbaud3front" />

                        </TableRow>

                        <TableRow
                            android:id="@+id/TableRow02"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/TextView04"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=" "
                                android:textAppearance="?android:attr/textAppearanceLarge" />
                        </TableRow>

                    </TableLayout>
                </ScrollView>

            </RelativeLayout> 

второй холст, который изменит изображение, — это

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >




                <ImageView
                    android:id="@+id/ImageView01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginLeft="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/girbaud1front" />

                <TextView
                    android:id="@+id/txt1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_marginBottom="62dp"
                    android:layout_marginLeft="48dp"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#000000" />

            </RelativeLayout>

java файл

         package com.example.flip3d;

               import android.app.Activity;
            import android.content.Intent;
            import android.graphics.drawable.Drawable;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.ImageView;
            import android.widget.Toast;


            public class Cards extends Activity {

                ImageView img1;
                ImageView img2;
                ImageView image;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.cards);

                    img1 =(ImageView) findViewById(R.id.img1);
                    img2 =(ImageView) findViewById(R.id.img2);
                    image =(ImageView) findViewById(R.id.ImageView01);

                    img1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View arg0) {

                            image.setImageResource(R.drawable.myfirstimage);
                            Intent startyou = new Intent("com.example.flip3d.FLIP3D");
                            startActivity(startyou);
                        }
                    });

                    img2.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View view) {





                            image.setImageResource(R.drawable.may2ndimage);

                            Toast.makeText(Cards.this, "Card Selected", Toast.LENGTH_SHORT).show();

                            Intent startyou = new Intent("com.example.flip3d.FLIP3D");
                            startActivity(startyou);
                        }
                    });




                      }


               }

http://i820.photobucket.com/albums/zz130/lestah23/untitled-1_zps33fa9be5.png
мое сообщение logcat.

когда я запускаю этот код на своем телефоне. произошла ошибка. и когда я проверяю logcat, он говорит. что я имеет ошибку в setImageResource();

что мне делать? im a biginner в программировании для Android: (

заблаговременно

Issue

I am trying to create a recycleView with image and toolbar. this is my adapter

public class ImageAdabter extends RecyclerView.Adapter<ImageViewHolder> {

private Context mContext;
private List<Integer> listImages;

public ImageAdabter(Context mContext) {
    this.mContext = mContext;
    this.listImages=getImages();
}

private List<Integer> getImages() {
    List<Integer> results = new ArrayList<>();
    results.add(R.drawable.ballon);
    results.add(R.drawable.owl);
    results.add(R.drawable.fish);
    return results;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_images,parent,false);
    return new ImageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position){holder.imageView.setImageResource(listImages.get(position));
}

@Override
public int getItemCount() {
    return listImages.size();
}

and i create a ImageViewHolder class . this is may code

public class ImageViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;
public ImageViewHolder(@NonNull View itemView) {
    super(itemView);
    itemView= itemView.findViewById(R.id.image_outline);

}

My main class code is

 initTollbar();
    initView();
}

private void initView() {
    recyclerView = findViewById(R.id.recycle_view_images);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adabter = new ImageAdabter(this);
    recyclerView.setAdapter(adabter);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initTollbar() {
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    int toolbar_item_color= ResourcesCompat.getColor(getResources(),R.color.blue,null);
    toolbar.setTitleTextColor(toolbar_item_color);
    toolbar.setSubtitleTextColor(toolbar_item_color);
    getSupportActionBar().setTitle("Mes Images");
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.close);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if(id == android.R.id.home){
        finish();
    }
    return super.onOptionsItemSelected(item);
}

when i run my code i got this error

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setImageResource(int)’ on a null object reference
.adabters.ImageAdabter.onBindViewHolder(ImageAdabter.java:44)
.adabters.ImageAdabter.onBindViewHolder(ImageAdabter.java:17)

thanks

Solution

Use below code

public class ImageViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;
public ImageViewHolder(@NonNull View itemView) {
    super(itemView);
    imageView = itemView.findViewById(R.id.image_outline);

}

Answered By – Sumit Kumar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Понравилась статья? Поделить с друзьями:
  • Setcooperativelevel ошибка epsxe
  • Setconsolecp 1251 ошибка
  • Setconfig application обнаружена ошибка windows xp
  • Sniper ghost warrior выдает ошибку
  • Sniper ghost warrior 3 ошибка при установке