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

标签 android

这是一个简单的代码,用于显示带有图像和文本的简单 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/

相关文章:

java - 在回收站 View 中切换到下一个 EditText 时,如何保持软键盘向上?

android - android中的SearchView自定义字体

java - 无法读取刚刚写入的文件

android - Android Jellybean (PhoneGap) 上的空白键盘输入?

java - 如果包含其他数据则显示列表 AlertDialog

android - 如何让另一个应用程序在 Android 中显示通知?

android - 更改的 Android 项目名称现在根本不起作用

android - 单体机器人 : Global Error Handler

Android - 在屏幕外放置 View

android - 从android studio将项目添加到github时出错