0

アプリケーションの目的は、スピナーを左 (ホーム チーム) と右 (アウェイ チーム) に配置することです。ホームチームのスピナーを正常に設定できますが、アウェイチームのスピナーを設定しようとするとエラーが発生します:

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;


}
}
4

1 に答える 1

0

json.org( http://www.json.org/javadoc/org/json/JSONArray.html#getJSONObject(int) )によると:

インデックスの値がない場合、または値が JSONObject でない場合、getJSONObject は JSONException をスローします。

ateamアレイはどのように設定されていますか? nullその中に値を追加しているようです。ateam配列に initialized のみが設定されていることを確認してください。配列に追加するときはJSONObjectおそらくJSONObjectisであるため、に追加する前にis かどうかを確認する必要があります。nullateamnullateam

于 2013-06-25T17:23:24.963 に答える