安卓listview item单击事件跳转空指针问题

两个activity源码,
mainactivity:
package com.example.newsclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.gxfi.entity.News;
import com.example.gxfi.service.GetXmlAndParse;

import android.R.string;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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:
package com.example.newsclient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

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);
}

}

错误:
图片说明

查看全部
qq_35361178
qq_35361178
2016/06/19 15:25
  • 点赞
  • 收藏
  • 回答
    私信

2个回复