0

我仍在研究该教程,但无法使其正常工作。错误发生在第 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});

}

4

1 回答 1

0

setAdapter() 方法是 a 的方法,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);
于 2013-11-01T12:37:44.173 回答