2016-03-24 26 views
0

私はAndroid開発者には新しく、いくつかの助けが必要です。JSON配列をリストビューに読み込む

私はJSONから情報をロードするアジェンダを構築しています。その後、ListViewはカスタムアダプタで膨らまされています。私はこれをして、うまく動作します。

連絡先をクリックすると、別のアクティビティに同じJSONを使用して、ユーザーに関する詳細情報が読み込まれます。私はそれをデバッグし、それがこのような情報をrecieves:

Example Item: [{"id":1,"name":"Leanne Graham","hobby":"Play soccer","address":"Kulas Light, Gwenborough","phone":"1-770-736-8031 x56442"}] 

私はそれがJSONArrayするキャストJSONObjectなどの情報を送っているので、私は私のrequestComplete私のアプリの休憩にその配列を渡すとき。

エラーは次のとおりです。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 

/**主な活動のonclickリスナー*/

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    System.out.println("POSITION: " + position); 
    JSONObject jsonObject = (JSONObject)JSONadapter.getItem(position); 


    Intent intent = new Intent(this, InfoActivity.class); 
    String pos_json = jsonObject.toString(); 
    intent.putExtra("pos_json",pos_json); 


    startActivity(intent); 

} 

/**情報活動*/

public class InfoActivity extends AppCompatActivity implements JSONRequest.JSONCallback { 

AdapterInfo JSONAdapter; 
private ListView listInfo; 
private JSONObject json_object; 
private JSONArray arrayMain; 

private ArrayList<String> jsonarray; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_info); 

    JSONArray array = new JSONArray(); 

    try { 
     json_object = new JSONObject(getIntent().getStringExtra("pos_json")); 
     arrayMain = array.put(json_object); 


     System.out.println("Example Item: "+ arrayMain.toString()); 
     System.out.println(arrayMain.getClass().getName()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    requestComplete(arrayMain); 


    this.listInfo = (ListView) findViewById(R.id.listView2); 


} 



@Override 
public void requestComplete(JSONArray array) { 
    JSONAdapter = new AdapterInfo(InfoActivity.this,array); 
    this.listInfo.setAdapter(JSONAdapter); 


} 

/** *アダプタ/

public class AdapterInfo extends BaseAdapter{ 

private JSONArray array; 
private Activity infoAct; 

public AdapterInfo(Activity infoAct, JSONArray array){ 
    this.array = array; 
    this.infoAct = infoAct; 
} 


@Override 
public int getCount() { 
    if(array == null){ 
     return 0; 
    }else{ 
     return array.length(); 
    } 
} 

@Override 
public JSONObject getItem(int position) { 
    if(array == null){ 
     return null; 
    }else{ 
     return array.optJSONObject(position); 
    } 

} 

@Override 
public long getItemId(int position) { 
    JSONObject object = getItem(position); 
    return object.optLong("id"); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView==null){ 
     convertView = infoAct.getLayoutInflater().inflate(R.layout.row,null); 

    } 

    TextView name = (TextView)convertView.findViewById(R.id.infoName); 
    TextView hobby = (TextView)convertView.findViewById(R.id.infoHobby); 
    TextView address = (TextView)convertView.findViewById(R.id.infoAddress); 
    TextView phone = (TextView)convertView.findViewById(R.id.infoPhone); 

    JSONObject json_data = getItem(position); 
    if(json_data != null){ 
     try { 
      String nombre = json_data.getString("name"); 
      String pasatiempo = json_data.getString("hobby"); 
      String direccion = json_data.getString("address"); 
      String telefono = json_data.getString("phone"); 

      name.setText(nombre); 
      hobby.setText(pasatiempo); 
      address.setText(direccion); 
      phone.setText(telefono); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
    return convertView; 
}} 

/** * JSONRequest/

public class JSONRequest extends AsyncTask<String, Void, JSONArray> { 

private JSONCallback callback; 

public JSONRequest(JSONCallback callback){ 
    this.callback = callback; 
} 


@Override 
protected JSONArray doInBackground(String... params) { 

    URLConnection connection = null; 
    BufferedReader br = null; 
    JSONArray result = null; 

    try{ 
     URL url = new URL(params[0]); 
     connection = (URLConnection) url.openConnection(); 


     InputStream is = connection.getInputStream(); 
     br = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder builder = new StringBuilder(); 
     String line = ""; 

     while((line = br.readLine()) != null){ 

      builder.append(line); 
     } 

     result = new JSONArray(builder.toString()); 

    }catch (Exception e) { 

     e.printStackTrace(); 
    } finally { 


     try{ 


      if(br != null) br.close(); 

     }catch(Exception e) { 

      e.printStackTrace(); 
     } 
    } 

    return result; 
} 

@Override 
protected void onPostExecute(JSONArray jsonArray) { 
    super.onPostExecute(jsonArray); 
    callback.requestComplete(jsonArray); 
} 


public interface JSONCallback{ 

    void requestComplete(JSONArray array); 
}} 
+0

エラーはJSONObjectとは関係ありません。それはnullであるlistInfoオブジェクトと関係があり、そのメソッドを呼び出すことを試みています。また、AndroidでSystem.outの呼び出しを避けてください。代わりにログを使用してください。 – chRyNaN

答えて

1

あなたのコード:

requestComplete(arrayMain); 

this.listInfo = (ListView) findViewById(R.id.listView2); 

requestComplete()this.listInfoインスタンスを使用しますが、それはrequestComplete()後に設定されているためthis.listInfoはnullです。だから、彼らの順序を切り替える必要があります。

this.listInfo = (ListView) findViewById(R.id.listView2); 
requestComplete(arrayMain); 

あなただけのただthis.listInfoが有効ListViewインスタンスを保持していることを確認するために、右setContentView()への呼び出し後にそれを置けばそれは良いです。

関連する問題