多个Spinners进行各种JSON调用

时间:2013-06-25 17:07:43

标签: android json spinner

该应用程序的目标是让旋转器(主队)和右侧(客队)。我可以成功填充主队的旋转器,但当我尝试填充客队球员时,我得到了错误:

06-25 18:02:36.052: W/System.err(17916): org.json.JSONException: Value at 0 is null.
06-25 18:02:36.057: W/System.err(17916):    at org.json.JSONArray.get(JSONArray.java:259)
06-25 18:02:36.057: W/System.err(17916):    at org.json.JSONArray.getJSONObject(JSONArray.java:480)
06-25 18:02:36.057: W/System.err(17916):    at app.league.npd.CaptainsInfo.useaJson(CaptainsInfo.java:309)

我可以看到错误来自“ateam”数组,但我不知道问题是什么。我是Android编程的新手,所以任何帮助都会受到赞赏。

以下是代码:

public class CaptainsInfo extends Activity {

JSONArray jsonArray = null;
JSONArray str_login = null;
public String items[];
public String aitems[];
private Spinner 
spinner1, spinner2, spinner3, spinner4, 
spinner5, spinner6, spinner7, spinner8, 
spinner9, spinner10, spinner11, spinner12,
spinner13, spinner14;
private Button btnSubmit;
public String kode;
public String Home_team;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    //addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
    addListenerOnSpinner2ItemSelection();

}


public void addItemsOnSpinner(String items[]) {

    spinner3 = (Spinner) findViewById(R.id.player11);
    spinner5 = (Spinner) findViewById(R.id.player12);
    spinner7 = (Spinner) findViewById(R.id.player21);
    spinner9 = (Spinner) findViewById(R.id.player22);
    spinner11 = (Spinner) findViewById(R.id.player31);
    spinner13 = (Spinner) findViewById(R.id.player32);




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner3.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
                this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner5.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
               this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner7.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner9.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner11.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner13.setAdapter(adapter);



    } 

public void addItemsOnSpinner2(String aitems[]) {

    spinner4 = (Spinner) findViewById(R.id.aplayer11);
    spinner6 = (Spinner) findViewById(R.id.aplayer12);
    spinner8 = (Spinner) findViewById(R.id.aplayer21);
    spinner10 = (Spinner) findViewById(R.id.aplayer22);
    spinner12 = (Spinner) findViewById(R.id.aplayer31);
    spinner14 = (Spinner) findViewById(R.id.aplayer32);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner4.setAdapter(adapter);
         adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner6.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
                this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner8.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner10.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
               this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner12.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item, aitems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner14.setAdapter(adapter);


}
public void addListenerOnSpinnerItemSelection(){

    spinner1 = (Spinner) findViewById(R.id.h_type);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
        {
            //String test1 = parent.getItemAtPosition(position).toString();
            String test = (String) spinner1.getSelectedItem();
            //String test2 = (String) spinner2.getSelectedItem();

            items=useJson(test);
            //aitems=useaJson(test2);


            //addItemsOnSpinner2(aitems);
            addItemsOnSpinner(items);


        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        } });}

    public void addListenerOnSpinner2ItemSelection(){

        spinner2 = (Spinner) findViewById(R.id.a_type);
        spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
            {
                //String test2 = parent.getItemAtPosition(position).toString();
                //String test = (String) spinner1.getSelectedItem();
                String test2 = (String) spinner2.getSelectedItem();

               // items=useJson(test);
                aitems=useaJson(test2);


                addItemsOnSpinner2(aitems);
                //addItemsOnSpinner(items);


            }



            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            } });
}







//get the selected dropdown list value
public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.h_type);
    spinner2 = (Spinner) findViewById(R.id.a_type);
    spinner3 = (Spinner) findViewById(R.id.player11);
    spinner4 = (Spinner) findViewById(R.id.aplayer11);
    spinner5 = (Spinner) findViewById(R.id.player12);
    spinner6 = (Spinner) findViewById(R.id.aplayer12);
    spinner7 = (Spinner) findViewById(R.id.player21);
    spinner8 = (Spinner) findViewById(R.id.aplayer21);
    spinner9 = (Spinner) findViewById(R.id.player22);
    spinner10 = (Spinner) findViewById(R.id.aplayer22);
    spinner11 = (Spinner) findViewById(R.id.player31);
    spinner12 = (Spinner) findViewById(R.id.aplayer31);
    spinner13 = (Spinner) findViewById(R.id.player32);
    spinner14 = (Spinner) findViewById(R.id.aplayer32);


    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(CaptainsInfo.this,
                    "OnClickListener : " + 
                    "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
                    "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                    Toast.LENGTH_SHORT).show();
        }

    });









 String links_url = "http://192.168.9.59/NPD/detail-info.php?Match_id="+kode;

// Call the FunctionParser to parse the information being returned
// From the FHL databaseList<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.FunctionParser(links_url);

// The array is called and the information is returned from
// the database corresponding to each string below
try {
str_login = json.getJSONArray("temp");
String jdwl = "";
TextView isi = (TextView) findViewById(R.id.fixdet);
for(int i = 0; i < str_login.length(); i++){
    JSONObject ar = str_login.getJSONObject(i);

    Home_team = ar.getString("Home_team");
    jdwl += "" + ar.getString("Home_team")+"\n vs \n"+
            "" + ar.getString("Away_team")+"\n"+
            "" + ar.getString("Fixture_date")+"\n"; 
}
isi.setText(jdwl);

} catch (JSONException e) {
e.printStackTrace();
}
} 

public String[] useJson(String test) {
     JSONParser jParser = new JSONParser();
        String link_url = "http://10.2.0.0/NPD/P_name.php?"+test;
        JSONObject json = jParser.FunctionParser(link_url);

        try {
            jsonArray = json.getJSONArray("team");
            items = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);           
            items[i]=jsonObject.getString("name");
            }
            return(items);}
    catch (JSONException e) {
        e.printStackTrace();
    }
        return items;



}

public String[] useaJson(String test2) {
     JSONParser jjParser = new JSONParser();
        String links_url = "http://10.2.0.0/NPD/P_name1.php?"+test2;
        JSONObject json = jjParser.FunctionParser(links_url);

        try {
            jsonArray = json.getJSONArray("ateam");
            aitems = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);           
            aitems[i]=jsonObject.getString("name");
            }
            return(aitems);}
   catch (JSONException e) {
        e.printStackTrace();
    }
        return aitems;


}
}

1 个答案:

答案 0 :(得分:0)

根据json.orghttp://www.json.org/javadoc/org/json/JSONArray.html#getJSONObject(int)):

  

如果索引没有值,则getJSONObject会抛出JSONException   或者如果值不是JSONObject。

您的ateam数组是如何填充的?似乎您在其中添加了null个值。确保仅在初始化ateam时填充JSONObject数组,将JSONObject添加到null数组时,ateam可能是null,因此您必须检查在将其添加到ateam之前{{1}}。