gpt4 book ai didi

android - R.id 无法解析或不是字段。

转载 作者:行者123 更新时间:2023-11-29 01:50:52 25 4
gpt4 key购买 nike

这是一个简单的代码,用于显示带有图像和文本的简单 ListView 。它给出了两个错误。 在声明中,

int[] to = { R.id.flag,R.id.txt}; //error: flag cannot be resolved or is not a fied
lv = ( ListView ) findViewById(R.id.listview); //error: listview cannot be resolved or is not a field.

希望您能接纳我!问候

 package com.example.amjad_3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

// Array of strings storing country names
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};

// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.india,
R.drawable.pakistan,
R.drawable.srilanka,
R.drawable.china,
R.drawable.bangladesh,
R.drawable.nepal,
R.drawable.afghanistan,
R.drawable.nkorea,
R.drawable.skorea,
R.drawable.japan
};

// Array of strings to store currencies

ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);

hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}

// Keys used in Hashmap
String[] from = { "flag","txt","cur" };

// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt};

// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview, from, to);

// Getting a reference to listview of main.xml layout file
lv = ( ListView ) findViewById(R.id.listview);

// Setting the adapter to the listView
lv.setAdapter(adapter);
}
}

最佳答案

希望这对你有帮助:

Buddy 你的代码中 from arrayto array 之间的映射错误..
因为你的 from array 有 3 个对象而你的 to array 有 2 个对象..
所以引发了这个异常..

只需更改代码中的这一行并再次运行您的项目:

// Keys used in Hashmap
String[] from = { "flag","txt" };

关于android - R.id 无法解析或不是字段。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18186672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com