2016-03-24 31 views
0

Android devresinde oldukça yeni ve yardıma ihtiyacım var.JSON dizisini listeleme listesine okuma

Bir JSON'dan bilgileri yükleyen bir ajanda yapıyorum, daha sonra bir ListView, özel bir adaptörle şişirildi. Bunu yaptım ve gayet iyi çalışıyor.

Başka bir kişiyi tıklattığımda sorunum şudur: Etkinlik, aynı JSON kullanılarak kullanıcı hakkında daha fazla bilgi yüklüdür. Bunu hata ayıklama ve bu gibi bilgileri recieves:

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

Ben bir JSONArray olmak döküm JSONObject nesnesi gibi bilgileri gönderilen Çünkü, ama benim requestComplete benim app sonları bu diziyi geçerken.

hatadır:

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

/** Esas faaliyet onclick dinleyici */

@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); 

} 

/** Bilgi etkinliği */

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); 


} 

/** Adaptör */

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

Bu hata JSONObject ile ilgisi yoktur. Bu, listInfo nesnesinin null olmasıyla ilgilidir ve üzerinde bir yöntem çağırmaya çalışıyorsunuz. Ayrıca, Android'de System.out çağrılarından kaçının; Bunun yerine Log kullan. – chRyNaN

cevap

1

Kodunuz:

requestComplete(arrayMain); 

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

requestComplete()this.listInfo örneğini kullanır ama requestComplete() sonra ayarlandığı için this.listInfo boş. Bu yüzden siparişlerini değiştirmelisin.

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

sadece sadece this.listInfo geçerli ListView örneğini tutan emin olmak için hemen setContentView() çağrısının koymak daha iyidir.

İlgili konular