将OnTouchListener
设置为相对布局,让我NullPointerException
。我不知道原因。
以下是代码:
holder.alert_message.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
((GroupChatActivity) context).expandAlertRow(
position,holder.alert_message,holder.WholeView);
holder.arrowAlertButton.setRotation(360);
return false;
}
});
代码遍历return语句后崩溃并提供NullPointerException
。
以下是日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.widget.ListView.measureScrapChild(ListView.java:1210)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
at android.widget.ListView.onMeasure(ListView.java:1202)
at android.view.View.measure(View.java:18596)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
at android.view.View.measure(View.java:18596)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:18596)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:18596)
at android.widget.ListView.setupChild(ListView.java:1968)
at android.widget.ListView.makeAndAddView(ListView.java:1889)
at android.widget.ListView.fillSpecific(ListView.java:1364)
at android.widget.ListView.layoutChildren(ListView.java:1671)
at android.widget.AbsListView.onLayout(AbsListView.java:2627)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2032)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
首先我以为我回来假这是问题但是在返回之后我得到了同样的Exception
。我已经google了很多但是找不到原因。这里回答
Returning true and false in OnTouch?
这是我的getview()
方法代码
public View getView(final int position, View convertView, ViewGroup parent) {
final MessageModel messageModel = bubbleMessageList.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_bubble_filling_adapter,
parent, false);
holder.senderName = (TextView) convertView.findViewById(R.id.sender_name_tv);
holder.arrowAlertButton = (Button) convertView.findViewById(R.id.arrow_alert_button);
holder.cancelAlertButton = (Button) convertView.findViewById(R.id.cancel_alert_button);
holder.unreadCounter = (Button) convertView.findViewById(R.id.unreadCounterBtn);
holder.Rl_ExpandView = (RelativeLayout) convertView.findViewById(R.id.Rl_ExpandView);
holder.alert_message = (RelativeLayout) convertView.findViewById(R.id.alert_message);
holder.WholeView = (RelativeLayout) convertView.findViewById(R.id.WholeView);
holder.attachmentButton = (ImageButton) convertView.findViewById(R.id.btn_Attachment);
holder.sendButton = (ImageButton) convertView.findViewById(R.id.btn_Send);
holder.messageEditText = (EditText) convertView.findViewById(R.id.edt_message);
holder.popupView=(View)convertView.findViewById(R.id.popupView);
holder.cancelAlertButton.setTag(position);
holder.alert_message.setTag(position);
holder.arrowAlertButton.setTag(position);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
setName(messageModel, holder);
if (messageModel.groupId !=null) {
if (!holderMapper.containsKey(messageModel.groupId)) {
//please don't delete this
/* Collections.sort(bubbleMessageList, new Comparator<MessageModel>() {
@Override
public int compare(MessageModel messageModel, MessageModel t1) {
return t1.sendOn.compareTo(messageModel.sendOn);
}
});*/
holderMapper.put(messageModel.groupId, holder);
}
} else {
if (!holderMapper.containsKey(messageModel.senderId))
holderMapper.put(messageModel.senderId, holder);
}
/* if (!holderMapper.containsKey(messageModel.groupId) ) {
if (messageModel.groupId != null)
holderMapper.put(messageModel.groupId, holder);
else
holderMapper.put(messageModel.senderId, holder);
}*/
holder.sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!holder.messageEditText.getText().toString().trim().isEmpty()) {
if (App.getCurrentActivity() instanceof GroupChatActivity) {
if (messageModel.groupId != null)
((GroupChatActivity) context).sendBubbleMessage(
holder.messageEditText.getText().toString(),
ConstantsEnum.messageType.TextType.getValue(),
ConstantsEnum.contentType.Text.getValue(),
null, messageModel.groupId, "", "", null,
ConstantsEnum.subMessageType.Normal.getValue());
else
((GroupChatActivity) context).sendBubbleMessageOneToOneCase(
holder.messageEditText.getText().toString(),
ConstantsEnum.messageType.TextType.getValue(),
ConstantsEnum.contentType.Text.getValue(),
null, messageModel.groupId, "", "", null,
ConstantsEnum.subMessageType.Normal.getValue());
} else {
((ChatActivity) context).sendBubbleMessage(
holder.messageEditText.getText().toString(),
ConstantsEnum.messageType.TextType.getValue(),
ConstantsEnum.contentType.Text.getValue(),
null, messageModel.groupId, "", "", null,
ConstantsEnum.subMessageType.Normal.getValue());
}
holder.messageEditText.setText("");
} else
AppUtility.showAlert(context, context
.getString(R.string.error), context
.getString(R.string.enter_text_to_send_message),
context.getString(R.string.ok));
}
});
holder.arrowAlertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (messageModel.isExpand) {
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
else
((ChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
int indexToRemove = 0;
int pos = (Integer) view.getTag();
if (messageModel.groupId != null) {
int groupID = bubbleMessageList.get(pos).groupId;
if (trackerForGroupId.containsKey(groupID)) {
trackerForGroupId.remove(groupID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.groupId == groupID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
holderMapper.remove(groupID);
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
} else {
int senderID = bubbleMessageList.get(pos).senderId;
if (trackerForGroupId.containsKey(senderID)) {
trackerForGroupId.remove(senderID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.senderId == senderID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
//((ChatActivity) context).updateSprowtCounter(senderID);
}
} else {
if (App.getCurrentActivity() instanceof GroupChatActivity) {
((GroupChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
int pos = (Integer) view.getTag();
int groupID = bubbleMessageList.get(pos).groupId;
((GroupChatActivity) context).updateSprowtCounter(groupID);
} else {
((ChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
}
}
holder.arrowAlertButton.setRotation(360);
}
});
holder.cancelAlertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//[Removing bubble row from the list]
int indexToRemove=0;
int pos = (Integer) view.getTag();
if (bubbleMessageList.get(pos).groupId != null) {
int groupID = bubbleMessageList.get(pos).groupId;
if (trackerForGroupId.containsKey(groupID)) {
trackerForGroupId.remove(groupID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.groupId == groupID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
holderMapper.remove(groupID);
if (App.getCurrentActivity() instanceof ChatActivity)
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
} else {
int senderID = bubbleMessageList.get(pos).senderId;
if (trackerForGroupId.containsKey(senderID)) {
trackerForGroupId.remove(senderID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.senderId == senderID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
if (App.getCurrentActivity() instanceof ChatActivity)
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
}
//[End removing bubble row from the list]
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).restoreLayoutToOriginalState();
else
((ChatActivity) context).restoreLayoutToOriginalState();
}
});
holder.attachmentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context,holder.popupView);
popupMenu
.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.captureImage:
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).dispatchTakePictureIntent();
else
((ChatActivity) context).dispatchTakePictureIntent();
break;
case R.id.imageGallery:
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).takePictureFromGallery();
else
((ChatActivity) context).takePictureFromGallery();
break;
case R.id.recordVideo:
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).startMakingVideo();
else
((GroupChatActivity) context).startMakingVideo();
break;
case R.id.selectVideo:
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).takeVideoFromGallery();
else
((GroupChatActivity) context).takeVideoFromGallery();
break;
}
return true;
}
});
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
}
});
if (messageModel.isExpand)
holder.Rl_ExpandView.setVisibility(View.VISIBLE);
else
holder.Rl_ExpandView.setVisibility(View.GONE);
holder.alert_message.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (messageModel.isExpand) {
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
else
((ChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
int indexToRemove = 0;
int pos = (Integer) view.getTag();
if (messageModel.groupId != null) {
int groupID = bubbleMessageList.get(pos).groupId;
if (trackerForGroupId.containsKey(groupID)) {
trackerForGroupId.remove(groupID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.groupId == groupID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
holderMapper.remove(groupID);
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
} else {
int senderID = bubbleMessageList.get(pos).senderId;
if (trackerForGroupId.containsKey(senderID)) {
trackerForGroupId.remove(senderID);
for (MessageModel messageModel1 : bubbleMessageList)
if (messageModel1.senderId == senderID)
indexToRemove = bubbleMessageList.indexOf(messageModel1);
bubbleMessageList.remove(indexToRemove);
if (App.getCurrentActivity() instanceof GroupChatActivity)
((GroupChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
else
((ChatActivity) context).bubbleFillingAdapter.notifyDataSetChanged();
}
//((ChatActivity) context).updateSprowtCounter(senderID);
}
} else {
if (App.getCurrentActivity() instanceof GroupChatActivity) {
((GroupChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
//int pos = (Integer) view.getTag();
/* int groupID = bubbleMessageList.get(pos).groupId;
int id = ((GroupChatActivity) context).msgForGroup.sproutGroupId;
((GroupChatActivity) context).updateSprowtCounter(id);*/
} else {
/* MessageModel myMessageModel = new MessageModel();
myMessageModel.sproutGroupId = messageModel.groupId;
UserSession.getInstance().addQueueMessage(myMessageModel);*/
((ChatActivity) context).expandAlertRow(
position, holder.alert_message, holder.WholeView);
}
}
holder.arrowAlertButton.setRotation(360);
return true;
}
});
if (messageModel.groupId != null)
setCounter(messageModel.groupId);
else setCounter(messageModel.senderId);
return convertView;
}