关于 学习安卓的过程中,关于SampleAdapter的问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

关于 学习安卓的过程中,关于SampleAdapter的问题

package com.aa.cao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter simp_adapter;
private List>datalist;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview.setAdapter(simp_adapter);
    simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
    datalist=new ArrayList<Map<String,Object>>();
}

private List<Map<String,Object>> getData(){
    for(int i=0;i<20;i++){
        Map<String,Object>map=new HashMap<String, Object>();
        map.put("pic", R.drawable.ic_launcher);
        map.put("text", "慕课网"+i);
        datalist.add(map);
    }
    return datalist;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

不知道为什么,一运行就报错,实在找不出问题

展开
收起
爵霸 2016-03-05 14:48:23 2289 0
1 条回答
写回答
取消 提交回答
  • 爵霸
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview.setAdapter(simp_adapter);
        simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
        datalist=new ArrayList<Map<String,Object>>();
    }

    listview要先实例化,如listview = (ListView)findViewById(R.id.xxx),xxx是listview的id
    同样的道理simp_adapter也要在 listview.setAdapter之前实例化

    2019-07-17 18:53:29
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
手机卫士性能优化方案-Android篇
立即下载
360手机卫士:插件化方案漫谈
立即下载
ReactNative框架在京东无线端的实践
立即下载