我的应用程序崩溃,因为我得到了INDEXOUTOFBOUNDSEXCEPTION。
item = feedItems.get(position);是什么超出界限。
我在服务器上保存了这个JSON文件:
{
"feed": [
{
"id": 1,
"name": "Atlanta",
"image": "http://i.huffpost.com/gen/1716876/thumbs/o-ATLANTA-TRAFFIC-facebook.jpg",
"status": "\"Enter here to start chatting.\"",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": null
},
{
"id": 2,
"name": "Chicago",
"image": "http://www.sagaftra.org/files/sag/branch_photos/chicago_banner.jpg",
"status": "30 years of Cirque du Soleil's best photos",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": "http://ti.me/1qW8MLB"
},
{
"id": 3,
"name": "Los Angelos",
"image": "http://www.wordexpress.net/wp-content/uploads/2013/10/Los-Angeles.jpg",
"status": "A team of Austrian scientists has developed a laser system that causes fruit flies to dance.",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": "http://dsc.tv/xmMxD"
},
{
"id": 4,
"name": "Houston",
"image": "http://media-cdn.tripadvisor.com/media/photo-s/04/bf/13/3c/inn-at-the-ballpark.jpg",
"status": "Android Sliding Menu using Navigation Drawer",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": "http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/"
},
{
"id": 5,
"name": "Miami",
"image": "http://i.huffpost.com/gen/1477351/thumbs/o-SKYRISE-MIAMI-OBSERVATION-TOWER-facebook.jpg",
"status": "That some achieve great success, is proof to all that others can achieve it as well",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": null
},
{
"id": 6 ,
"name": "Las Vegas",
"image": "http://www.tinaalberino.com/wp-content/uploads/2014/09/vegas-burj-al-arab.jpg",
"status": "\"The Beast\" KTM 1290 Super Duke",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": ""
},
{
"id": 7,
"name": "New York",
"image": "http://www.burgessyachts.com/media/adminforms/locations/n/e/new_york_1.jpg",
"status": "We’re assembling riders of every style, bike, and passion. If you ride with conviction, ride with us. You have 24 days to get ready for World Ride. Prepare by visiting:",
"profilePic": "http://clxxxii.vm-host.net/clxxxii/dubchat.png",
"timeStamp": "1403375851930",
"url": "http://bit.ly/1wmBWaN"
}
]
}
我使用过这些教程。
http://www.androidhive.info/2014/10/android-building-group-chat-app-using-sockets-part-2/
http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/
这是"定制适配器"我在下面进行了调整。信使应用程序的适配器类不会用完条目。套接字服务器继续显示用户发送给服务器的文本。在Facebook教程中,它的适配器只能容纳四个条目。因此,当我组合适配器时,我已经将消息显示在Facebook状态消息上。问题是当您发送超过7条消息时,应用程序崩溃是因为JSON文件条目的长度。
所以当我实现这个小代码时。
if (position >= 6) {
position = 0;
}
它将位置重置为0,但只有当我向服务器发送消息时,应用程序才会崩溃,这意味着我可以发送超过7条消息。这里的问题是状态消息只显示我输入的第一条消息。这意味着我在第7个参赛作品后输入的任何内容都没有显示出来。
item = feedItems.get(position);是什么超出界限。
public class MessagesListAdapter extends BaseAdapter implements
View.OnClickListener {
private Context context;
private List<Message> messagesItems;
private List<FeedItem> feedItems;
MediaPlayer mp;
NetworkImageView profilePic;
FeedImageView feedImageView;
TextView name;
FeedItem item;
String postitionINDEX;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public MessagesListAdapter(Context context, List<Message> navDrawerItems) {
this.context = context;
this.messagesItems = navDrawerItems;
}
public MessagesListAdapter(Context context, List<Message> navDrawerItems,
List<FeedItem> feedItems) {
this.context = context;
this.messagesItems = navDrawerItems;
this.feedItems = feedItems;
}
public int getCountFeed() {
return feedItems.size();
}
public Object getItemFeed(int location) {
return feedItems.get(location);
}
public long getItemIdFeed(int position) {
return position;
}
@Override
public int getCount() {
return messagesItems.size();
}
@Override
public Object getItem(int position) {
return messagesItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* The following list not implemented reusable list items as list items
* are showing incorrect data Add the solution if you have one
* */
Message m = messagesItems.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// Identifying the message owner
if (messagesItems.get(position).isSelf()) {
// message belongs to you, so load the right aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_arrived,
null);
} else {
// message belongs to other person, load the left aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_sent,
null);
}
TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
// txtMsg.setText(m.getMessage());
txtMsg.setText(Integer.toString(position));
lblFrom.setText(m.getFromName());
//
//
//
//
//
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
name = (TextView) convertView.findViewById(R.id.name);
TextView timestamp = (TextView) convertView
.findViewById(R.id.timestamp);
TextView statusMsg = (TextView) convertView
.findViewById(R.id.txtStatusMsg);
TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
profilePic = (NetworkImageView) convertView
.findViewById(R.id.profilePic);
feedImageView = (FeedImageView) convertView
.findViewById(R.id.feedImage1);
// postitionINDEX = feedItems.get(position).getStatus();
// postitionINDEX = Integer.toString(feedItems.get(position).getId() +
// 1);
// statusMsg.setText(m.getMessage());
item = feedItems.get(position);
// statusMsg.setText(postitionINDEX);
// name.setText(item.getName());
// final int ok234 = messagesItems.indexOf(1);
// int index = feedItems.indexOf("papa"); // index = 2
// name.setText(Integer.toString(ok234));
// name.setText(Integer.toString(item.getId()));
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(item.getTimeStamp()),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
timestamp.setText(timeAgo);
// Chcek for empty status message
if (!TextUtils.isEmpty(item.getStatus())) {
statusMsg.setText(m.getMessage());
statusMsg.setVisibility(View.VISIBLE);
} else {
// status is empty, remove from view
statusMsg.setVisibility(View.GONE);
}
// Checking for null feed url
if (item.getUrl() != null) {
url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
+ item.getUrl() + "</a> "));
// Making url clickable
url.setMovementMethod(LinkMovementMethod.getInstance());
// URL HIDDEN FOR NOW
url.setVisibility(View.GONE);
} else {
// url is null, remove from the view
url.setVisibility(View.GONE);
}
// user profile pic
profilePic.setImageUrl(item.getProfilePic(), imageLoader);
// Feed image
if (item.getImge() != null) {
feedImageView.setImageUrl(item.getImge(), imageLoader);
feedImageView.setVisibility(View.VISIBLE);
feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
} else {
feedImageView.setVisibility(View.GONE);
}
profilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (item.getId()) {
case 1:
mp = MediaPlayer.create(context, R.raw.cameraflash);
mp.setVolume(100, 100);
mp.start();
Intent myIntent1 = new Intent(context, NameActivity.class);
myIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent1);
break;
case 2:
mp = MediaPlayer.create(context, R.raw.cameraflash);
mp.setVolume(100, 100);
mp.start();
Intent myIntent2 = new Intent(context, NameActivity.class);
myIntent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent2);
break;
case 3:
mp = MediaPlayer.create(context, R.raw.cameraflash);
mp.setVolume(100, 100);
mp.start();
Intent myIntent3 = new Intent(context, NameActivity.class);
myIntent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent3);
break;
}
}
});
feedImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return convertView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
这是承载适配器类的Activity中的JSON解析方法。
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (i5 = 0; i5 < feedArray.length(); i5++) {
JSONObject feedObj = (JSONObject) feedArray.get(i5);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
item.setName(feedObj.getString("name"));
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImge(image);
item.setStatus(feedObj.getString("status"));
item.setProfilePic(feedObj.getString("profilePic"));
item.setTimeStamp(feedObj.getString("timeStamp"));
// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);
feedItems.add(item);
}
//
i5 = 0;
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
我可以更改JSON对象中的每个条目,然后继续将项添加到JSON文件中。或者我无法使用JSON文件并创建一个永不停止并在您离开应用程序时被删除的动态文件。有谁知道解决这个应用程序的好方法?