0

Im using a matrix cursor to add rows to cursor.

Defined columns in the MainActivity on Create

//In protected void onCreate

String[] columns = new String[] { "dealername", "product","type","description","location","sublocation","address","phone1","phone2","auth","brands","lat","lon"};
cursor= new MatrixCursor(columns);

//----------------- In asyctask doinbackground

cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

When i try to get number of columns in the cursor the app simply crashes

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
}

UPDATE:

 private class getdealerinfo extends AsyncTask<Void,Void,MatrixCursor> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected  MatrixCursor doInBackground(Void... args) {

                /* Building Parameters */

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pname",selectedproduct));
                params.add(new BasicNameValuePair("location",selectedlocation));

                /* getting JSON string from URL */
                JSONObject json = jParser.makeHttpRequest(dealers_url, "GET", params);

                try {
                    /* Checking for SUCCESS TAG */
                    int success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        JSONArray JAStuff = json.getJSONArray(TAG_STUFF);

                        /** CHECK THE NUMBER OF RECORDS **/
                        int intStuff = JAStuff.length();
                        //creating array 
                        dealerdetailsarray =new String[intStuff][12];
                        if (intStuff != 0) {
                             String[] str1 = new String[JAStuff.length()]; 
                             for(int i=0;i<JAStuff.length();i++)
                             {
                             json=JAStuff.getJSONObject(i);
                            // startManagingCursor(cursor);
                  //         dealerdetailsarray[0][0]=json.getString("dealername");
                             cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

                             } 
                             }
                    }
                } catch (Exception e) {
              Log.e("test",e.toString());
                }
                return cursor; 
            }


            @Override
            protected void onPostExecute(MatrixCursor cursor) {
            //    super.onPostExecute(aVoid);
             try
             {
              Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
             }
             catch (Exception e) {
                  Log.e("myerror",e.getMessage());
                    } 
       }
        }

    }
7
  • are you getting NullPointerException? Commented Mar 9, 2015 at 10:57
  • @pskink I tried adding catch (Exception e) { Log.e("test",e.toString()); } But nothing is catched. Commented Mar 9, 2015 at 11:02
  • what is the stack trace? Commented Mar 9, 2015 at 11:03
  • @pskink Logcat gives nothing.Please see the update Commented Mar 9, 2015 at 11:16
  • and where do you create your cursor? Commented Mar 9, 2015 at 11:19

1 Answer 1

1

You need to return the cursor object from doInBackground() to onPostExecute(). Define your AsyncTask class as

class XYZTask extends AsyncTask<Void, Void, MatrixCursor>{

    protected MatrixCursor doInBackground(Void... void) {

         .....
         cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});
         return cursor;
    }

    protected void onPostExecute(MatrixCursor cursor) {
        Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
    }


}

and return the cursor object from doInBackground() as a parameter to onPostExecute().

EDIT:

Define your AsyncTask like this:

private class getdealerinfo extends AsyncTask<Void,Void,JSONObject> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected  JSONObject doInBackground(Void... args) {

                /* Building Parameters */

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pname",selectedproduct));
                params.add(new BasicNameValuePair("location",selectedlocation));

                /* getting JSON string from URL */
                JSONObject json = jParser.makeHttpRequest(dealers_url, "GET", params);


                return json; 
            }


            @Override
            protected void onPostExecute(JSONObject json) {
            //    super.onPostExecute(aVoid);
            MatrixCursor cursor = new MatrixCursor();

             try {
                    /* Checking for SUCCESS TAG */
                    int success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        JSONArray JAStuff = json.getJSONArray(TAG_STUFF);

                        /** CHECK THE NUMBER OF RECORDS **/
                        int intStuff = JAStuff.length();
                        //creating array 
                        dealerdetailsarray =new String[intStuff][12];
                        if (intStuff != 0) {
                             String[] str1 = new String[JAStuff.length()]; 
                             for(int i=0;i<JAStuff.length();i++)
                             {
                             json=JAStuff.getJSONObject(i);
                            // startManagingCursor(cursor);
                  //         dealerdetailsarray[0][0]=json.getString("dealername");
                             cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

                             } 
                             }
                    }
                } catch (Exception e) {
              Log.e("test",e.toString());
                }
             Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();

       }
        }

    }

Try this first.

Sign up to request clarification or add additional context in comments.

9 Comments

hmm... How? Please add more details
Please see the update.I tried catching the error Logcat shows nothing
without the logcat error it will be extremely difficult to help you ... so far i am basing my answers on guesswork and trying to pin down the common sources or error.
i tried using the tag 'test' .But nothing is printed.Is there any way i can see more details
in your IDE settings make sure that ALL logcat messages, including the lowest-priority ones, are displayed
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.