我是Android新手。我的listview中有391项。所有工作正常,直到390,它是可见的,但当我向下滚动查看最后的项目,我的应用程序崩溃。帮帮我。提前谢谢。
MainActivity:
public class Companies extends Activity {
ListView list_com;
String value[];
String c_city, c_state;
String company, address, phone, email, website;
CompanyAdapter com_ad;
ProgressDialog pDialog;
EditText ed_search;
String com_name1[];
String com_name, com_id, city, state, city1[], state1[], com_id1[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_companies);
list_com = (ListView) findViewById(R.id.list_company);
//ed_search = (EditText) findViewById(R.id.ed_search);
new AttemptCompany().execute();
}
class AttemptCompany extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Companies.this);
pDialog.setMessage("Loading....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// Check for success tag
try {
// Building Parameters
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Parser parser = new Parser();
JSONObject json = parser.getJSONFromUrl(
"http://.....",
nameValuePairs);
Log.d("Login attempt", json.toString());
int udata = json.getInt("udata");
if (udata == 1) {
city = json.getString("city");
state = json.getString("state");
city1 = city.split(",");
state1 = state.split(",");
JSONArray udata1 = json.getJSONArray("result");
for (int i = 0; i < udata1.length(); i++) {
JSONObject json1 = (JSONObject) udata1.get(i);
if (i == 0) {
com_name = (i + 1) + ". "
+ json1.getString("company") + ",";
com_id = json1.getString("id") + ",";
} else {
com_name += (i + 1) + ". "
+ json1.getString("company") + ",";
com_id += json1.getString("id") + ",";
}
}
com_name1 = com_name.split(",");
com_id1 = com_id.split(",");
return null;
}
if (udata == 0) {
/*
* Toast.makeText(getApplicationContext(), "No data found",
* Toast.LENGTH_SHORT).show();
*/
return "No data found";
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", "" + e.toString());
return "No data found";
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Companies.this, file_url, Toast.LENGTH_LONG)
.show();
}
else {
Log.e("log_tag", "Hello");
com_ad = new CompanyAdapter(Companies.this, com_name1, city1,state1);
list_com.setAdapter(com_ad);
list_com.setTextFilterEnabled(true);
}
}
}
}
适配器:
public class CompanyAdapter extends ArrayAdapter<String> {
String companyname[];
String city[];
String state[];
Context context;
TextView tv_city, tv_company, tv_state;
public CompanyAdapter(Context context, String[] companyname, String[] city,
String[] state) {
super(context, R.layout.listview, companyname);
this.context = context;
this.city = city;
this.state = state;
this.companyname = companyname;
}
@SuppressLint("ViewHolder")
public View getView(final int positon, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.companylist, parent, false);
tv_company = (TextView) rowView.findViewById(R.id.company_name1);
tv_city = (TextView) rowView.findViewById(R.id.cityList);
tv_state = (TextView) rowView.findViewById(R.id.stateList);
tv_company.setText(companyname[positon]);
tv_city.setText(city[positon]);
tv_state.setText(state[positon]);
return rowView;
}
}
Company.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fddcaa"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/bg_color_image"
android:text="Companies Name"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000" />
<ListView
android:id="@+id/list_company"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
适配器XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#fddcaa"
android:weightSum="1" >
<TextView
android:id="@+id/company_name1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".60"
android:gravity="top"
android:text="S.No"
android:textStyle="bold"
android:paddingLeft="10dp"
android:textColor="#000"
android:textSize="16sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight=".3"
android:weightSum="2">
<TextView
android:id="@+id/cityList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="3dp"
android:gravity="center_vertical"
android:text="Name"
android:paddingLeft="5dp"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/stateList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:text="D.O.B"
android:textColor="#000"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
logcat的:
java.lang.ArrayIndexOutOfBoundsException: length=392; index=392
01-07 11:31:25.576: E/AndroidRuntime(13317): at com.company.android.CompanyAdapter.getView(CompanyAdapter.java:41)
01-07 11:31:25.576: E/AndroidRuntime(13317): at android.widget.AbsListView.obtainView(AbsListView.java:2347)
01-07 11:31:25.576: E/AndroidRuntime(13317): at android.widget.ListView.makeAndAddView(ListView.java:1864)
01-07 11:31:25.576: E/AndroidRuntime(13317): at android.widget.ListView.fillDown(ListView.java:698)
01-07 11:31:25.576: E/AndroidRuntime(13317): at android.widget.ListView.fillGap(ListView.java:662)
答案 0 :(得分:0)
替换为,
public View getView(final int positon, View convertView, ViewGroup parent) {
if (city.length <= position)
return null;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.companylist, parent, false);
tv_company = (TextView) rowView.findViewById(R.id.company_name1);
tv_city = (TextView) rowView.findViewById(R.id.cityList);
tv_state = (TextView) rowView.findViewById(R.id.stateList);
tv_company.setText(companyname[positon]);
tv_city.setText(city[positon]);
tv_state.setText(state[positon]);
return rowView;
}