开发者社区> 问答> 正文

listview 用baseadapter报空指针?报错

报错如下

代码如下


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就会报空指针


这是为什么


展开
收起
爱吃鱼的程序员 2020-06-20 17:03:15 497 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    ....超级无语的问题。有没有破的方法<divclass="linealt2">

    myaccount=MainActivity.userInfo.getAccount();
     不带这样共享变量的要怎么共享变量?没经验自己写的

    2020-06-20 17:03:31
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载