我仍在that tutorial工作,我无法让它发挥作用。错误发生在第38行lv.setAdapter(simpleAdpt);
我的整个MainActivity.java可以在下面看到。如果有人可以帮助修复此错误并告诉我它为什么会发生,我终于可以运行该应用程序了。我用38行测试了应用程序,但应用程序在启动后崩溃了。谢谢你的帮助。我还是一个新手。
package com.example.lern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
//Data to Show
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
@SuppressWarnings("unused")
private void initList() {
// Populating them planets
planetsList.add(createPlanet("planet", "Mercury"));
planetsList.add(createPlanet("planet", "Venus"));
planetsList.add(createPlanet("planet", "Earth"));
planetsList.add(createPlanet("planet", "Mars"));
planetsList.add(createPlanet("planet", "Jupiter"));
planetsList.add(createPlanet("planet", "Saturn"));
planetsList.add(createPlanet("planet", "Uranus"));
planetsList.add(createPlanet("planet", "Neptune"));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Object lv;
lv.setAdapter(simpleAdpt);
}
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);
return planet;
}
//This is a simple adapter
//The keys are used to retrieve the data
//the View id is Used to show the data. The <key number and the view id must match
SimpleAdapter simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[]
{"planet"}, new int[] {android.R.id.text1});
}
答案 0 :(得分:0)
setAdapter()方法是ListView
的方法,未定义类型Object
Object lv;
lv.setAdapter(simpleAdpt);
正确地遵循教程..
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);