0

Crash compile this code.

This is HOME.java

public class Home extends ActionBarActivity {



    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> empresaList;


    // url to get all products list
    private static String url_all_empresas = "http://my_url/get_all_empresas.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "empresas";
    //private static final String TAG_ID = "id";
    private static final String TAG_NOMBRE = "nombre";
    private static final String TAG_TITULAR = "titular";
    private static final String TAG_DIRECCION = "direccion";
    private static final String TAG_LOCALIDAD = "localidad";
    private static final String TAG_TELEFONO = "telefono";

    // products JSONArray
    JSONArray products = null;

    ListView lista;







    private AdView mAdView;

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

        setContentView(R.layout.dia_siguiente);
        AdView mAdView = (AdView) findViewById(R.id.ad_view);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);




        // Hashmap para el ListView
        empresaList = new ArrayList<HashMap<String, String>>();

        // Cargar los productos en el Background Thread
        new LoadAllProducts().execute();
        lista = (ListView) findViewById(R.id.listAllProducts);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

    }//fin onCreate


    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Antes de empezar el background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Home.this);
            pDialog.setMessage("Cargando... Por favor espere...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * obteniendo todos los productos
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List params = new ArrayList();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_empresas, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    //Log.i("ramiro", "produtos.length" + products.length());
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                       // String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NOMBRE);
                        String titular = c.getString(TAG_TITULAR);
                        String direccion = c.getString(TAG_DIRECCION);
                        String localidad = c.getString(TAG_LOCALIDAD);
                        String telefono = c.getString(TAG_TELEFONO);


                        // creating new HashMap
                        HashMap map = new HashMap();

                        // adding each child node to HashMap key => value
                       // map.put(TAG_ID, id);
                        map.put(TAG_NOMBRE, name);
                        map.put(TAG_TITULAR, titular);
                        map.put(TAG_DIRECCION, direccion);
                        map.put(TAG_LOCALIDAD, localidad);
                        map.put(TAG_TELEFONO, telefono);

                        empresaList.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            Home.this,
                            empresaList,
                            R.layout.single_post,
                            new String[] {
                                   // TAG_ID,
                                    TAG_NOMBRE,
                                    TAG_TITULAR,
                                    TAG_DIRECCION,
                                    TAG_LOCALIDAD,
                                    TAG_TELEFONO,
                            },
                            new int[] {
                                    //R.id.single_post_tv_id,
                                    R.id.single_post_tv_nombre,
                                    R.id.single_post_tv_titular,
                                    R.id.single_post_tv_direccion,
                                    R.id.single_post_tv_localidad,
                                    R.id.single_post_tv_telefono,
                            });
                    // updating listview
                    //setListAdapter(adapter);
                    lista.setAdapter(adapter);

                }
            });
        }
    }
}

I get the following error when attempting to run it:

********************LOGCAT***********************************************

08-18 01:23:13.904    8565-8565/com.disemur.farmaguar E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.disemur.farmaguar, PID: 8565
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at com.disemur.farmaguar.Home$LoadAllProducts$1.run(Home.java:211)
            at android.app.Activity.runOnUiThread(Activity.java:5575)
            at com.disemur.farmaguar.Home$LoadAllProducts.onPostExecute(Home.java:185)
            at com.disemur.farmaguar.Home$LoadAllProducts.onPostExecute(Home.java:106)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Tordek
  • 10,628
  • 3
  • 36
  • 67
jose
  • 11
  • 1
  • Have you tried a debugger? – D. Ben Knoble Aug 17 '15 at 23:39
  • IMHO it's only one probable issue. Error log says that listview is null. check if I'd given to findviewbyid method on lista is proper Id and try to move loading executing after findviewbyid – Sebastian Pakieła Aug 18 '15 at 00:20
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – njzk2 Aug 18 '15 at 01:09

2 Answers2

0

Most likely this statement doesn't execute as you expect.

lista = (ListView) findViewById(R.id.listAllProducts);

Check the result using debugger or Log.

Sweder Schellens
  • 410
  • 1
  • 4
  • 14
0

Post R.layout.activity_homecode also
I suspect you are providing incorrect listview Id

You've set the view setContentView twice on your Activity's onCreate() method.

Find which layout is correct and the listview should be defined in your currently inflated layout.

Set the correct layout in your onCreate() method

Adnan
  • 1,440
  • 2
  • 18
  • 38
  • It may be more useful to the OP and for future reference to include some code here as an illustration of your answer. – ManoDestra Apr 06 '16 at 16:28