我有一个问题,我在一个屏幕上有两个微调器,两个都在JSON解析后填充,第二个微调器在从第一个微调器中选择项后填充;我想要;第一次打开屏幕然后微调器显示选择关键字,但是当我们想要在第一个微调器中更改所选项时,它会显示所有已解析的数据。但在尝试这样做后,我收到了一条错误消息。我不知道怎么会被删除?我希望你对这个问题表示友好。
错误:
09-19 14:27:39.103: ERROR/AndroidRuntime(807): FATAL EXCEPTION: main
09-19 14:27:39.103: ERROR/AndroidRuntime(807): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at java.util.ArrayList.get(ArrayList.java:311)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.Spinner.makeAndAddView(Spinner.java:189)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.Spinner.layout(Spinner.java:148)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.Spinner.onLayout(Spinner.java:112)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1238)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.onLayout(LinearLayout.java:1044)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.View.layout(View.java:7035)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.os.Looper.loop(Looper.java:123)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:521)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-19 14:27:39.103: ERROR/AndroidRuntime(807): at dalvik.system.NativeStart.main(Native Method)
代码在这里
package com.equinix.android.sitevisit;
import java.util.ArrayList;
import java.util.Calendar;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;
import com.equinix.android.adapter.SiteVisit_CagesAdapter;
import com.equinix.android.adapter.SiteVisit_IBXAdapter;
import com.equinix.android.common.LoginScreen;
import com.equinix.android.common.MenuScreen;
import com.equinix.android.common.R;
import com.equinix.android.parsing.Parse_Json;
import com.equinix.android.servicecall.Service_Call;
public class Site_Visit_Details extends Activity {
/** Called when the activity is first created. */
ImageButton _siteVisit_Home, _siteVisit_Logout, _siteVisit_AddVisitor, _siteVisit_StartDateButton, _siteVisit_EndDateButton, _siteVisit_Next, _siteVisit_CheckBox;
EditText _siteVisit_StartDate, _siteVisit_EndDate, _siteVisit_AdditionalInfo;
Spinner _siteVisit_IBX, _siteVisit_Cage;
ArrayAdapter statusAdapter;
Bundle bundle;
private int mYear;
private int mMonth;
private int mDay;
private int flagValue, checkBoxFlag=0;
static final int DATE_DIALOG_ID = 0;
public static int count=0;
public static ArrayList<String> cage_Id = new ArrayList<String>();
public static ArrayList<String> cage_Name = new ArrayList<String>();
public static ArrayList<String> cage_NaturalKey = new ArrayList<String>();
public static boolean flag_cages = false;
public static ArrayList<String> firstName = new ArrayList<String>();
public static ArrayList<String> lastName = new ArrayList<String>();
public static ArrayList<String> companyName = new ArrayList<String>();
SharedPreferences prefs;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.site_visit_details);
bundle = getIntent().getExtras();
_siteVisit_Home = (ImageButton)findViewById(R.id.home);
_siteVisit_Logout = (ImageButton)findViewById(R.id.logout);
_siteVisit_AddVisitor = (ImageButton)findViewById(R.id.addVisitorImageButton);
_siteVisit_StartDateButton = (ImageButton)findViewById(R.id.startDateImageButton);
_siteVisit_EndDateButton = (ImageButton)findViewById(R.id.endDateImageButton);
_siteVisit_Next = (ImageButton)findViewById(R.id.nextImageButton);
_siteVisit_CheckBox =(ImageButton)findViewById(R.id.checkBox);
_siteVisit_StartDate = (EditText)findViewById(R.id.startDateEditText);
_siteVisit_EndDate = (EditText)findViewById(R.id.endDateEditText);
_siteVisit_AdditionalInfo = (EditText)findViewById(R.id.additionalInformationEditText);
_siteVisit_IBX = (Spinner)findViewById(R.id.ibxSpinner);
_siteVisit_Cage = (Spinner)findViewById(R.id.cageSpinner);
LoginScreen.name.add("Select");
statusAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item , LoginScreen.name);//Sets an status adapter
statusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_siteVisit_IBX.setAdapter(statusAdapter);
_siteVisit_Cage.setAdapter(statusAdapter);
//Get Data from Parsing
try{
HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/ibx");
StringEntity se = new StringEntity("{\"credentials\":{\"username\":\"mobileuser1\",\"password\":\"welcome1\"}}");
/* HttpPost post = new HttpPost("http://192.168.0.9/DEVELOPMENT/VERSIONED/Equinix/trunk/iphone/login");
StringEntity se = new StringEntity("{\"ibx\":{\"credentials\":{\"password\":\"welcome1\",\"userid\":\"mobileuser1\"}}}");*/
final Service_Call obj = new Service_Call(post, se, "mobileuser1", "welcome1",false);
final ProgressDialog dialog = ProgressDialog.show(Site_Visit_Details.this, " EQUINIX ", "Loading.... ", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
statusAdapter.notifyDataSetChanged();
dialog.dismiss();
}
};
final Thread checkUpdate = new Thread() {
public void run() {
LoginScreen.name.clear();
String json_string = obj.call_Service();
new Parse_Json(json_string).parse();
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
}catch(Exception e)
{
e.printStackTrace();
}
/* Intent intent = new Intent(LoginScreen.this,MenuScreen.class);
startActivity(intent);*/
//statusAdapter.add(LoginScreen.name);
SiteVisit_IBXAdapter adapter = new SiteVisit_IBXAdapter(LoginScreen.name, this);
if(bundle.getInt("screen_flag")!=0)
{
try{
prefs = getSharedPreferences(PREFS_NAME, 0);
String savedIBXString = prefs.getString("savedIBXString",null);
String savedCagesString = prefs.getString("savedCagesString", null);
String startDate = prefs.getString("startDate", null);
String endDate= prefs.getString("endDate", null);
String addInfo = prefs.getString("additionalInfo", null);
_siteVisit_StartDate.setText(startDate);
_siteVisit_EndDate.setText(endDate);
_siteVisit_AdditionalInfo.setText(addInfo);
for(int i=0;i<LoginScreen.name.size();i++)
{
if(savedIBXString.equals(_siteVisit_IBX.getItemAtPosition(i).toString()))
{
_siteVisit_IBX.setSelection(i);
}
}
for(int i=0;i<cage_Name.size();i++)
{
if(savedCagesString.equals(_siteVisit_Cage.getItemAtPosition(i).toString()))
{
_siteVisit_Cage.setSelection(i);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
_siteVisit_Home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, MenuScreen.class);
startActivity(intent);
}
});
try {
firstName = bundle.getStringArrayList("FirstName");
lastName = bundle.getStringArrayList("LastName");
companyName = bundle.getStringArrayList("Company");
for(int i=0; i<firstName.size(); i++){
System.out.println("Name is ======== "+firstName.get(i)+" "+lastName.get(i)+" Company name=====>> "+companyName.get(i));
}
} catch (Exception e1) {
e1.printStackTrace();
}
_siteVisit_Logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, LoginScreen.class);
startActivity(intent);
}
});
_siteVisit_Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try{
System.out.println(" Size of ArrayList is "+firstName.size());
if(firstName.size()!=0){
if((_siteVisit_StartDate.getText().toString().length()!=0) &&(_siteVisit_EndDate.getText().toString().length()!=0))
{
Intent intent = new Intent(Site_Visit_Details.this, Site_Visit_Login.class);
intent.putExtra("IBXName", _siteVisit_IBX.getSelectedItem().toString());
intent.putExtra("CagesName", _siteVisit_Cage.getSelectedItem().toString());
intent.putExtra("StartDate", _siteVisit_StartDate.getText().toString());
intent.putExtra("EndDate", _siteVisit_EndDate.getText().toString());
intent.putStringArrayListExtra("FirstName", firstName);
intent.putStringArrayListExtra("LastName", lastName);
intent.putStringArrayListExtra("Company", companyName);
startActivity(intent);
}else{
Toast.makeText(Site_Visit_Details.this, "Please fill all mandatory field", Toast.LENGTH_SHORT).show();
}
}
else{
AlertDialog alertDialog = new AlertDialog.Builder(Site_Visit_Details.this).create();
alertDialog.setTitle("EQUINIX");
alertDialog.setMessage("Please Add Visitor First");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
}catch(Exception e)
{
Toast.makeText(Site_Visit_Details.this, "Please select add visitor", Toast.LENGTH_SHORT).show();
}
}
});
_siteVisit_IBX.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item was selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
try{
arrayList_clear();
SiteVisit_IBXAdapter adapter = new SiteVisit_IBXAdapter(LoginScreen.name, Site_Visit_Details.this);
_siteVisit_IBX.setAdapter(adapter);
prefs = PreferenceManager.getDefaultSharedPreferences( getBaseContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",_siteVisit_IBX.getSelectedItem().toString());
HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/cages");
StringEntity se = new StringEntity("{\"ibx\":{\"credentials\":{\"username\":\"mobileuser1\",\"password\":\"welcome1\"},\"userkey\":\"68782\",\"permissionCode\":\"PLACE_BASIC_SERVICES\",\"id\":\""+LoginScreen.id.get(pos)+"\",\"name\":\""+LoginScreen.name.get(pos)+"\",\"naturalKey\":\""+LoginScreen.naturalKey.get(pos)+"\"}}");
final Service_Call obj = new Service_Call(post, se, "mobileuser1", "welcome1",false);
/*String json_Response = obj.call_Service();
new Parse_Json(json_Response).parse_Cages();*/
/*SiteVisit_CagesAdapter adapter = new SiteVisit_CagesAdapter(Site_Visit_Details.cage_Name, Site_Visit_Details.this);
_siteVisit_Cage.setAdapter(adapter);*/
final ProgressDialog dialog = ProgressDialog.show(Site_Visit_Details.this, "EQUINIX ", "Loading.... ", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
SiteVisit_CagesAdapter adapter = new SiteVisit_CagesAdapter(Site_Visit_Details.cage_Name, Site_Visit_Details.this);
_siteVisit_Cage.setAdapter(adapter);
dialog.dismiss();
}
};
final Thread checkUpdate = new Thread() {
public void run() {
String json_Response = obj.call_Service();
new Parse_Json(json_Response).parse_Cages();
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
private void arrayList_clear() {
Site_Visit_Details.cage_Id.clear();
Site_Visit_Details.cage_Name.clear();
Site_Visit_Details.cage_NaturalKey.clear();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
_siteVisit_Cage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item was selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
prefs = PreferenceManager.getDefaultSharedPreferences( getBaseContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedCagesString",_siteVisit_Cage.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
_siteVisit_StartDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
flagValue = 0;
showDialog(DATE_DIALOG_ID);
}
});
_siteVisit_EndDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
flagValue = 1;
showDialog(DATE_DIALOG_ID);
}
});
_siteVisit_AddVisitor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, Site_Visit_AddVisitor.class);
startActivity(intent);
}
});
_siteVisit_CheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(checkBoxFlag==0){
_siteVisit_CheckBox.setBackgroundResource(R.drawable.check_box_checked);
checkBoxFlag=1;
}else{
_siteVisit_CheckBox.setBackgroundResource(R.drawable.check_box_unchecked);
checkBoxFlag=0;
}
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay(flagValue);
}
};
private void updateDisplay(int flag) {
if(flag==0){
_siteVisit_StartDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" "));
}
else{
_siteVisit_EndDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" "));
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("savedIBXString", _siteVisit_IBX.getSelectedItem().toString());
editor.putString("savedCagesString", _siteVisit_Cage.getSelectedItem().toString());
editor.putString("startDate", _siteVisit_StartDate.getText().toString());
editor.putString("endDate", _siteVisit_EndDate.getText().toString());
editor.putString("additionalInfo", _siteVisit_AdditionalInfo.getText().toString());
// Commit the edits!
editor.commit();
}
}
答案 0 :(得分:0)
检查您在适配器中插入的ArrayList对象(如果它不为空),使用Logs,然后双击LogCat中的Error以将您重定向到您收到错误的代码行