qq_35361178
qq_35361178
采纳率0%
2016-06-19 23:33

安卓listview item单击事件跳转新的activity,intent中的value问空

对listview item加监听,单击跳转新得activity报空指针错误,value值明明放在intent里了,还是空指针错误
mainavtivity:
public class MainActivity extends Activity {
private List listNews;
private ListView list;

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch (msg.what) {
        case GetXmlAndParse.PARSESUCCWSS:
            listNews =  (List) msg.obj;
            initData();
            break;
        }
        super.handleMessage(msg);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.list);
    GetXmlAndParse getXmlAndParse = new GetXmlAndParse(mHandler);
    getXmlAndParse.getXml();
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            switch (parent.getId())
            {
            case R.id.list:
                expressItemClick(position);//position 代表你点的哪一个
                break;


        }
    }

        private void expressItemClick(int position) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this,NewsActivity.class);


          Map<String, String> item = (Map<String, String>) list.getAdapter().getItem(position);
           String essay = item.get("description");

            //添加到intent域中
            intent.putExtra("description",essay );
            startActivity(intent);

            finish();

        }});

}

/**
 * 将解析后的xml填充到ListView
 */
protected void initData() {
    // TODO Auto-generated method stub
    List items = new ArrayList();
    for (News news : (List<News>)listNews) {
        Map item = new HashMap();
        item.put("title", news.getTitle());
        item.put("description", news.getDescription());
        items.add(item);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.news_view,
            new String[] { "title" }, new int[] { R.id.textView1 });
    list.setAdapter(adapter);

}

}
newsactivity:
public class NewsActivity extends Activity {
private TextView myTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    String value = intent.getStringExtra("description");

    myTextView = (TextView) findViewById(R.id.textview2);
    // 
    myTextView.setText(value);
}

}
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • q610098308 恋恋西风 5年前

    有可能解析 xml ; item.put("title", news.getTitle()); news.getTitle()为空,所以后面取的也为空了;

    点赞 评论 复制链接分享
  • qq_34544496 练小芽 5年前

    你在expressItemClick方法里打印一下essay的值就知道有没有了

    点赞 评论 复制链接分享
  • zhijun_gis zhijun_gis 5年前

    NewsActivity的第20行是哪一行啊?

    点赞 评论 复制链接分享

为你推荐