listview 用baseadapter报空指针

KyJason 发布于 2014/06/22 19:04
阅读 927
收藏 0

报错如下

代码如下


public class ChatActivity extends Activity {

	private int myaccount;
	private int chat_account;
	private String chat_content;
	EditText mes_input;
	ListView chatList;
	MyBroadcastReceiver br;
	public List<ChatEntity> chatEntityList = new ArrayList<ChatEntity>();// 所有聊天内容

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_chat);

		chat_account = getIntent().getIntExtra("account", 0);
		ImageView avatar = (ImageView) findViewById(R.id.account_avatar1);
		avatar.setImageResource(R.drawable.avatar_default);
		TextView nick = (TextView) findViewById(R.id.account_nick1);
		nick.setText(getIntent().getStringExtra("nick"));

		mes_input = (EditText) findViewById(R.id.input_message);
		findViewById(R.id.back_button_contact).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						finish();
						startActivity(new Intent(ChatActivity.this,
								MainActivity.class));
					}

				});
		findViewById(R.id.send_message_button).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						myaccount = MainActivity.userInfo.getAccount();
					
						chat_content = mes_input.getText().toString();
						mes_input.setText("");

						String time = JQTime.getComTime();
							
						ChatEntity chat = new ChatEntity();
						chat.setContent(chat_content);
						chat.setLeft(false);
						chat.setTime(time);
						updateChatView(chat);
		
						message.setSendTime(time);
						oos.writeObject(message);							
					}

				});

		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction("my.jq.mes");
		br = new MyBroadcastReceiver();
		registerReceiver(br, myIntentFilter);
		ManageActivity.addActivity("ChatActivity", this);

	}

	public void finish() {
		unregisterReceiver(br);
		super.finish();
	}

	public class MyBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String[] mes = intent.getStringArrayExtra("message");
			ChatEntity chat = new ChatEntity();
			chat.setContent(mes[3]);
			chat.setLeft(true);
			chat.setTime(mes[4]);
			updateChatView(chat);
		}
	}

	public void updateChatView(ChatEntity chatEntity) {
		chatEntityList.add(chatEntity);
		chatList = (ListView) findViewById(R.id.list_chat);
		chatList.setAdapter(new ChatAdapter(this, chatEntityList));
	}
}


package jq.adapter;

import java.util.List;
import java.util.zip.Inflater;

import jq.activity.MainActivity;
import jq.activity.R;
import jq.entity.ChatEntity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChatAdapter extends BaseAdapter {

	public Context context;
	public List<ChatEntity> chatEntityList;
	LayoutInflater inflater;

	public ChatAdapter(Context context, List<ChatEntity> chatEntityList2) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.chatEntityList = chatEntityList2;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return chatEntityList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return chatEntityList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ChatEntity chatEntity = chatEntityList.get(position);
		ImageView avatar;
		TextView content;
		TextView time;
		Log.v("chatadapter","size  "+chatEntityList.size());
		if (chatEntity.isLeft()) {
			view = inflater.inflate(R.layout.chat_listview_item_left, null);
			view = inflater.inflate(R.layout.chat_listview_item_right, null);
			avatar = (ImageView) view.findViewById(R.id.avatar_chat_left);
			content = (TextView) view.findViewById(R.id.message_chat_left);
			time = (TextView) view.findViewById(R.id.sendtime_chat_left);

			
		} else {
			view = inflater.inflate(R.layout.chat_listview_item_right, null);
			avatar = (ImageView) view.findViewById(R.id.avatar_chat_right);
			content = (TextView) view.findViewById(R.id.message_chat_right);
			time = (TextView) view.findViewById(R.id.sendtime_chat_right);

		}


		avatar.setImageResource(R.drawable.avatar_default);
		content.setText(chatEntity.getContent());
		time.setText(chatEntity.getTime());
		

		return view;
	}

}




第一次执行onclick之后调用updateChatView可以显示

第二次执行onclick就会报空指针


这是为什么


加载中
0
nevans
nevans
....超级无语的问题。
KyJason
KyJason
有没有破的方法
0
fneg
fneg
myaccount = MainActivity.userInfo.getAccount();
  不带这样共享变量的
KyJason
KyJason
要怎么共享变量? 没经验 自己写的
返回顶部
顶部