通过putExtra将信息传递给intent

时间:2015-02-12 10:25:49

标签: android

您好我正在尝试使用put extra,因此我可以将listview中的用户名和位置发送给新意图,但我不确定为什么我的代码无效。该应用程序加载,但当我点击listitem它停止运行。任何帮助将不胜感激!!

public class MainActivity extends Activity {

    TextView user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        user = (TextView) findViewById(R.id.messaging_username);


        ListView listView = (ListView) this.findViewById(R.id.messagingListView);
        final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.GetActivityFeedData());

  View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
        listView.addFooterView(footerView);

        listView.setAdapter(itemAdapter);

        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Intent intent = new Intent(getApplicationContext(), ConversationView.class);
                intent.putExtra("username", user.getText().toString());
                intent.putExtra("pos", position);
                startActivity(intent);
                            }
        });
    }


    // Get dummy data for Activity Feed

    public ArrayList<MessageItem> GetActivityFeedData() {
        ArrayList<MessageItem> items = new ArrayList<MessageItem>();

        //testing fields
        items.add(new MessageItem(1, "Bob Jones", "image", DateTime.now(), "Hello how are you?"));
        items.add(new MessageItem(2, "Charlie Smith", "image", DateTime.now(), "Hello what are you doing"));
        items.add(new MessageItem(3, "Robert Cross", "image", DateTime.now(), "Hello how are you?"));
        return items;
    }


    class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {

        ArrayList<MessageItem> recentTracks;

        @Override
        protected Void doInBackground(Integer... page) {
            try {
                  recentTracks = new ArrayList<MessageItem>();

                Thread.sleep(3000);

                // loading infinite Data
                MessageItem data = null;
                for (int i = 0; i < 10; i++) {
                    data = new MessageItem(1, "John Smith", "image", DateTime.now(), "hello");


                    recentTracks.add(data);
                }
            } catch (Exception e) {
            }

            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            ListView listView = (ListView) findViewById(R.id.messagingListView);

            ActivityAdapter adapter = ((ActivityAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter());


            if (adapter == null) {
                adapter = new ActivityAdapter(getApplicationContext(), recentTracks);
                listView.setAdapter(adapter);
            } else {
                adapter.addAll(recentTracks);
                adapter.notifyDataSetChanged();
            }


        }

    }


    public class ActivityAdapter extends ArrayAdapter<MessageItem> {
        private final Context context;
        private final ArrayList<MessageItem> items;
        private int currentPage = 0;

        public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
            super(context, 0, recentTrackArrayList);
            this.context = context;
            this.items = recentTrackArrayList;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView;


            {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                final MessageItem item = items.get(position);

                convertView = getLayoutInflater().inflate
                        (R.layout.message_list_item, parent, false);

                //final MessageItem item1 = items.get(position);
                }



            if (position == getCount() - 1) {
                currentPage++;
                new ActivityFeedTask().execute(currentPage);
            }

            return convertView;
        }

    }

}

这是我删除getExtra

时的意图活动
public class ConversationView extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_view);

       Intent intent = getIntent();
       String name = intent.getStringExtra("username");
    }


   }

1 个答案:

答案 0 :(得分:0)

messaging_username TextView位于R.layout.message_list_item布局中ListView行布局,但您正在调用findViewById(R.id.messaging_username),因此可能会在user.getText().toString()行获取NPE。

使用view从ListView行的ListView行获取messaging_username TextView单击:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
user=(TextView)view.findViewById(R.id.messaging_username);
.....
startActivity(intent);
}
});