Project Android studio crashea app when finished listview load

0

good afternoon community, and happy year I would like to ask why when I finish loading a listview through a service in my project, instead of showing up, the app crashes directly? the test I'm doing with a G4 plus motorcycle (if you have something to do, in its edition of 2GB of ram) and the main files I'm using are these

Service

public class Servicio {

public ArrayList<Locales> GetData(){
    JSONArray JsonArr = null;
    ArrayList<Locales> locales = new ArrayList<Locales>();
    String dir = "http://192.168.0.3:8081/locales";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    URL url;
    HttpURLConnection connection;
    try
    {
        url = new URL(dir);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        BufferedReader ent = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = null;
        StringBuffer response = new StringBuffer();
        String json = "";
        while((line = ent.readLine()) != null)
        {
            response.append(line);
        }
        json = response.toString();
        JsonArr = new JSONArray(json);
        for (int i= 0; i<JsonArr.length();i++ ){
            JSONObject JsonObject = JsonArr.getJSONObject(i);
            Locales locales1 = new Locales();
            locales1.Nombre = JsonObject.optString("Nombre");
            locales1.Direccion = JsonObject.optString("Direccion");
            locales1.Valuacion = JsonObject.optInt("Valuacion");
            locales.add(locales1);
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return locales;
}

Adapter

public class LocalesAdapter extends ArrayAdapter<Locales> {
private ArrayList<Locales> objects;
private Context mContext;

public LocalesAdapter(Context context, ArrayList<Locales> objects) {
    super(context, 0, objects);
    this.mContext = context;
    this.objects = objects;
}

public View getView(int position, View convertView, ViewGroup parent){

    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }
    Locales i = objects.get(position);

    if (i != null) {
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
        TextView mt = (TextView) v.findViewById(R.id.middletext);
        TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        TextView btd = (TextView) v.findViewById(R.id.desctext);

        // check to see if each individual textview is null.
        // if not, assign some text!
        if (tt != null){
            tt.setText("Nombre: ");
        }
        if (ttd != null){
            ttd.setText(i.getNombre());
        }
        if (mt != null){
            mt.setText("Direccion: ");
        }
        if (mtd != null){
            mtd.setText("$" + i.getDireccion());
        }
        if (bt != null){
            bt.setText("Valuacion: ");
        }
        if (btd != null){
            btd.setText(i.getValuacion());
        }
    }

    // the view must be returned to our activity
    return v;

}

Main

public class MainActivity extends AppCompatActivity {
private WebSocketClient mWebSocketClient = null;
private Runnable viewParts = null;
private LocalesAdapter m_adapter= null;
private ArrayList<Locales> listaLocales = new ArrayList<Locales>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lv = (ListView)findViewById(R.id.mainListView);
    Servicio ser = new Servicio();
    listaLocales = ser.GetData();
    m_adapter = new LocalesAdapter(this, listaLocales);
    lv.setAdapter(m_adapter);
}

the issue is that just, the compiler does not show any error, it is more, it ends all the execution, but for some reason, the debugger does not load the logs of the device, and if I run it in the emulator, I crashed the emulator by the hardware of my machine, which does not give the power to run it correctly

    
asked by Pablo Ezequiel Ferreyra 03.01.2018 в 22:41
source

1 answer

0

Thanks to the comments of @PabloSimonDiEstefano, first, I had the following problems:

  

When you do super (context, 0, objects); in the constructor of your adapter > why do you spend 0? Pass the layout of your adapter with R.layout.locales_adapter.xml or whatever it's called in your case

and the other problem is that I was throwing an error in the adapter, when I passed the appraisal of the place I had, it seems that I was not the textlabel, and that crashed me the app too

        if (bt != null){
        bt.setText("Valuacion: ");
    }
    if (btd != null){
        btd.setText(i.getValuacion());
    }

temporarily, comment on this code fragment to continue and then correct it

    
answered by 04.01.2018 в 15:12