这段代码曾经工作但我添加了一些东西只是为了获取contentprovider名称[s]。无论我多少检查不会有任何空或空的arraylists,它仍然会出现同样的错误。
logcat的
05-25 08:57:40.429: E/AndroidRuntime(11600): FATAL EXCEPTION: main
05-25 08:57:40.429: E/AndroidRuntime(11600): Process: com.example.sillimaninquirer, PID: 11600
05-25 08:57:40.429: E/AndroidRuntime(11600): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
05-25 08:57:40.429: E/AndroidRuntime(11600): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
05-25 08:57:40.429: E/AndroidRuntime(11600): at java.util.ArrayList.get(ArrayList.java:308)
05-25 08:57:40.429: E/AndroidRuntime(11600): at com.example.sillimaninquirer.Adapter.getView(NewsFeed.java:402)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.AbsListView.obtainView(AbsListView.java:2467)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.ListView.measureHeightOfChildren(ListView.java:1286)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.ListView.onMeasure(ListView.java:1194)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:846)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:512)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
05-25 08:57:40.429: E/AndroidRuntime(11600): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
05-25 08:57:40.429: E/AndroidRuntime(11600): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2728)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.View.measure(View.java:17915)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2412)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1414)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1638)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.Choreographer.doFrame(Choreographer.java:572)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.os.Handler.handleCallback(Handler.java:815)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.os.Handler.dispatchMessage(Handler.java:104)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.os.Looper.loop(Looper.java:194)
05-25 08:57:40.429: E/AndroidRuntime(11600): at android.app.ActivityThread.main(ActivityThread.java:5631)
05-25 08:57:40.429: E/AndroidRuntime(11600): at java.lang.reflect.Method.invoke(Native Method)
05-25 08:57:40.429: E/AndroidRuntime(11600): at java.lang.reflect.Method.invoke(Method.java:372)
05-25 08:57:40.429: E/AndroidRuntime(11600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
05-25 08:57:40.429: E/AndroidRuntime(11600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
NewsFeed.java
import java.util.ArrayList;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class NewsFeed extends Activity {
ListView feed_listView;
EditText search;
//Button t, a, d, c, k;
Spinner spinner;
AQuery aq;
ProgressDialog pd;
Adapter artadapter;
JSONArray articles = new JSONArray();
ArrayList<String> artID = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> content = new ArrayList<String>();
ArrayList<String> date = new ArrayList<String>();
ArrayList<String> cpid = new ArrayList<String>();
ArrayList<String> publisher = new ArrayList<String>();
JSONArray contentprovider = new JSONArray();
ArrayList<String> cpID = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
spinner = (Spinner)findViewById(R.id.spinner1);
aq = new AQuery(this);
pd = new ProgressDialog(this);
pd.setMessage("Loading articles. . .");
pd.setCancelable(false);
getSIData();
getContentProvider();
search = (EditText) findViewById(R.id.search);
feed_listView = (ListView) findViewById(R.id.listView_NewsFeed);
artadapter = new Adapter(this,artID, title, content, date, publisher,cpid);
feed_listView.setAdapter(artadapter);
feed_listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String t = title.get(position);
String c = content.get(position);
String d = date.get(position);
String p = publisher.get(position);
String cp = cpid.get(position);
Intent i = new Intent(getApplicationContext(), Article.class);
i.putExtra("t", t);
i.putExtra("c", c);
i.putExtra("d", d);
i.putExtra("p", p);
i.putExtra("cp", cp);
startActivity(i);
Toast.makeText(getApplicationContext(), "Clicked "+position,Toast.LENGTH_LONG).show();
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchTitle(s.toString());
if(s.toString().equals("")){
clearVar();
addArticlesToVar();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void AddPref(View v){
Intent i = new Intent(v.getContext(), AddPreference.class);
startActivity(i);
}
public void News(View v){
Intent i = new Intent(v.getContext(), NewsFeed.class);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news_feed, menu);
return true;
}
public void getSIData(){
aq.progress(pd).ajax("http://sillimaninquirer.esy.es/TWS/sidata.php?key=none", JSONObject.class, new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject jo, AjaxStatus as){
try {
if(jo!=null){
articles = jo.getJSONArray("articles");
//Toast.makeText(getBaseContext(), "Articles: "+articles, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Error: No internet connection", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
addArticlesToVar();
}
});
}
public void getContentProvider(){
aq.progress(pd).ajax("http://sillimaninquirer.esy.es/TWS/sidata.php?key=contentprovider", JSONObject.class, new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject jo, AjaxStatus as){
try {
if(jo!=null){
contentprovider = jo.getJSONArray("contentprovider");
//Toast.makeText(getBaseContext(), "Articles: "+articles, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Error: No internet connection", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void addArticlesToVar(){
// articles = Manage.getArticles();
for(int x=0; x<articles.length(); x++){
try {
JSONObject article = articles.getJSONObject(x);
artID.add(article.getString("id"));
title.add(article.getString("title"));
content.add(article.getString("content"));
date.add(article.getString("pdate"));
String pub = article.getString("pubid");
if(pub=="1"){
publisher.add("Stones and Pebbles");
}
if(pub=="1"){
publisher.add("Junior Sillimanians");
}
if(pub=="1"){
publisher.add("the Weekly Sillimanian");
}
String a = article.getString("cpid");
for(int y=0; y<contentprovider.length(); y++){
try {
JSONObject cp = contentprovider.getJSONObject(y);
String con = article.getString("id");
if(a==con){
String fname = cp.getString("firstname");
String lname = cp.getString("lastname");
String mi = cp.getString("mi");
cpid.add(fname+" "+mi+". "+lname);
}
} catch (JSONException r) {
r.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
artadapter.notifyDataSetChanged();
Log.d("XX"," has art? "+articles);
}
public void searchTitle(String key){
clearVar();
for(int x=0; x<articles.length(); x++){
try {
JSONObject article = articles.getJSONObject(x);
String[] splited = article.getString("title").split(" "); //split on word boundries
if(Arrays.asList(splited).contains(key)){
artID.add(article.getString("id"));
title.add(article.getString("title"));
content.add(article.getString("content"));
date.add(article.getString("pdate"));
String pub = article.getString("pubid");
if(pub=="1"){
publisher.add("Stones and Pebbles");
}
if(pub=="1"){
publisher.add("Junior Sillimanians");
}
if(pub=="1"){
publisher.add("the Weekly Sillimanian");
}
String a = article.getString("cpid");
for(int y=0; y<contentprovider.length(); y++){
try {
JSONObject cp = contentprovider.getJSONObject(y);
String con = article.getString("id");
if(a==con){
String fname = cp.getString("firstname");
String lname = cp.getString("lastname");
String mi = cp.getString("mi");
cpid.add(fname+" "+mi+". "+lname);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
artadapter.notifyDataSetChanged();
}
public void searchDate(String key){
clearVar();
for(int x=0; x<articles.length(); x++){
try {
JSONObject article = articles.getJSONObject(x);
String[] splited = article.getString("pdtae").split(" "); //split on word boundries
if(Arrays.asList(splited).contains(key)){
artID.add(article.getString("id"));
title.add(article.getString("title"));
content.add(article.getString("content"));
date.add(article.getString("pdate"));
String pub = article.getString("pubid");
if(pub=="1"){
publisher.add("Stones and Pebbles");
}
if(pub=="1"){
publisher.add("Junior Sillimanians");
}
if(pub=="1"){
publisher.add("the Weekly Sillimanian");
}
String a = article.getString("cpid");
for(int y=0; y<contentprovider.length(); y++){
try {
JSONObject cp = contentprovider.getJSONObject(y);
String con = article.getString("id");
if(a==con){
String fname = cp.getString("firstname");
String lname = cp.getString("lastname");
String mi = cp.getString("mi");
cpid.add(fname+" "+mi+". "+lname);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
artadapter.notifyDataSetChanged();
}
public void clearVar(){
title.clear();
content.clear();
date.clear();
cpid.clear();
}
}
class Adapter extends ArrayAdapter<String>{
Context context;
ArrayList<String> artID, title, content, date, apubid, cpid;
//ArrayList<Articles> art1, art2;
Adapter (Context context, ArrayList<String> artID,ArrayList<String> title,ArrayList<String> content, ArrayList<String> date, ArrayList<String> apubid, ArrayList<String>cpid){
super(context, R.layout.row,R.id.newsfeed_Title, title);
this.context = context;
this.artID = artID;
this.content = content;
this.date = date;
this.title = title;
this.apubid = apubid;
this.cpid = cpid;
}
public View getView(int position, View convertView, ViewGroup parent){
String c="", con="";
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row, parent, false);
TextView tv_title = (TextView) row.findViewById(R.id.newsfeed_Title);
TextView tv_content = (TextView) row.findViewById(R.id.newsfeed_Contentext);
TextView tv_date = (TextView) row.findViewById(R.id.newsfeed_date);
c = content.get(position);
for(int cc=0; cc<50; cc++){
con+=c.charAt(cc);
}
tv_title.setText(title.get(position));
tv_content.setText(apubid.get(position)+"\n"+con);
tv_date.setText("By "+cpid.get(position)+" "+date.get(position));
return row;
}
}
答案 0 :(得分:0)
致电
artadapter = new Adapter(this,artID, title, content, date, publisher,cpid);
内容数组为空。这就是你遇到问题的原因