androidimagelistviewsimpleadapter

Android Can't set image in a ListView layout


Looks like find the image but i don't know how to insert into the simple adapter (or anything that let me set the image in my layout) https://gyazo.com/61ab064c19ae50090f08e6436776a52e

I want to set an image for each object in the array, like:

monsterimglist.setImageResource(R.mipmap.icon_name);

For the moment im testing to show R.mipmap.a1 in all the array items in the listview.

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

Someone can help me? Thanks a lot

ListView monsterslistview;
ImageView monsterimg;
TextView monstername;
TextView monstertype;

private static String monsterid;
ArrayList<HashMap<String, String>> MonstersList;


@Override
protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_monsters);

    //Fiquem els elements creats la seva id corresponent
    monstername = (TextView) findViewById(R.id.namelayout);
    monstertype = (TextView) findViewById(R.id.typelayout);
    monsterslistview = (ListView) findViewById(R.id.monsterslistview);
    monsterimg = (ImageView) findViewById(R.id.imglayout);


    HashMap<String, String> hashMap;

    //Creem un nou objecte ArrayList per inserir les dades
    MonstersList = new ArrayList<HashMap<String, String>>();

    //..........Process JSON DATA................
        try {
            JSONObject reader = new JSONObject(loadJSONFromAsset());

            //Guardem a un Array de tipus JSON les dades
            JSONArray jr = reader.getJSONArray("monsters");
            for (int i = 0; i < jr.length(); i++) {
                JSONObject jsonObjectLine = jr.getJSONObject(i);

                // Desem els items JSON en una variable
                String id = jsonObjectLine.getString("id");
                String name = jsonObjectLine.getString("name");
                String type = jsonObjectLine.getString("type");
                String icon = jsonObjectLine.getString("icon");

                //Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();

                // Afegim la clau-valor a un objecte HashMap
                hashMap = new HashMap<String, String>();
                hashMap.put("id", id);
                hashMap.put("name", name);
                hashMap.put("type", type);
                hashMap.put("icon", icon);
                MonstersList.add(hashMap);

                //mostrem per pantalla els elements que volem mostar
                ListAdapter adapter = new SimpleAdapter(
                        ListMonsters.this,
                        MonstersList,
                        R.layout.onemonster,
                        new String[]{"name", "type","icon"},
                        new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}

                );
                //Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
                monsterslistview.setAdapter(adapter);
                //String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
                monsterimg.setImageResource(R.mipmap.a1);




                monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        /**
                        String idagafat = MonstersList.get(position).get("id").replace(" ", "");
                        String positionagafada = String.valueOf(position);
                        openmonster(idagafat, positionagafada);
                         **/


                    }
                });

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


// Funció llegir json
public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = ListMonsters.this.getAssets().open("monsters.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

public void openmonster(String id, String pos){
    int posarray = Integer.parseInt(pos);


    Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
    //Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
    //Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show();
}

}


Solution

  • Close the Bracket before creating the adapter.May be it will change your result.

    Updated :

        ListView monsterslistview; ImageView monsterimg; TextView monstername; TextView monstertype;
    
    private static String monsterid; ArrayList<HashMap<String, String>> MonstersList;
    
    
    @Override protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_monsters);
    
        //Fiquem els elements creats la seva id corresponent
        monstername = (TextView) findViewById(R.id.namelayout);
        monstertype = (TextView) findViewById(R.id.typelayout);
        monsterslistview = (ListView) findViewById(R.id.monsterslistview);
        monsterimg = (ImageView) findViewById(R.id.imglayout);
    
    
        HashMap<String, String> hashMap;
    
        //Creem un nou objecte ArrayList per inserir les dades
        MonstersList = new ArrayList<HashMap<String, String>>();
    
        //..........Process JSON DATA................
            try {
                JSONObject reader = new JSONObject(loadJSONFromAsset());
    
                //Guardem a un Array de tipus JSON les dades
                JSONArray jr = reader.getJSONArray("monsters");
                for (int i = 0; i < jr.length(); i++) {
                    JSONObject jsonObjectLine = jr.getJSONObject(i);
    
                    // Desem els items JSON en una variable
                    String id = jsonObjectLine.getString("id");
                    String name = jsonObjectLine.getString("name");
                    String type = jsonObjectLine.getString("type");
                    String icon = jsonObjectLine.getString("icon");
    
                    //Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
    
                    // Afegim la clau-valor a un objecte HashMap
                    hashMap = new HashMap<String, String>();
                    hashMap.put("id", id);
                    hashMap.put("name", name);
                    hashMap.put("type", type);
                    hashMap.put("icon", icon);
                    MonstersList.add(hashMap);
    
        }   //-----This Bracket was missed in your code
    
    
                    //mostrem per pantalla els elements que volem mostar
                    ListAdapter adapter = new SimpleAdapter(
                            ListMonsters.this,
                            MonstersList,
                            R.layout.onemonster,
                            new String[]{"name", "type","icon"},
                            new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}
    
                    );
                    //Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
                    monsterslistview.setAdapter(adapter);
                    //String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
                    monsterimg.setImageResource(R.mipmap.a1);
    
    
    
    
                    monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            /**
                            String idagafat = MonstersList.get(position).get("id").replace(" ", "");
                            String positionagafada = String.valueOf(position);
                            openmonster(idagafat, positionagafada);
                             **/
    
    
                        }
                    });
    
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
    
    // Funció llegir json public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = ListMonsters.this.getAssets().open("monsters.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json; }
    
    public void openmonster(String id, String pos){
        int posarray = Integer.parseInt(pos);
    
    
        Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
        //Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
        //Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show(); }