看起来像找到图像,但我不知道如何插入简单的适配器(或任何让我在我的布局中设置图像的东西) https://gyazo.com/61ab064c19ae50090f08e6436776a52e
我想为数组中的每个对象设置一个图像,如:
monsterimglist.setImageResource(R.mipmap.icon_name);
我正在测试,以便在列表视图的所有数组项中显示R.mipmap.a1
。
错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageResource(int)'
有人可以帮帮我吗?非常感谢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();
}
}
答案 0 :(得分:0)
在创建适配器之前关闭支架。可能会改变您的结果。
更新:
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(); }