报错如下
代码如下
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就会报空指针
这是为什么
....超级无语的问题。有没有破的方法<divclass="linealt2">
myaccount=MainActivity.userInfo.getAccount(); |
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。