0

I am passing the value to another activity but getting always null value

public class SatelliteDirectActivity extends Activity {
private Intent intent;
private Bundle b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    intent = new Intent(SatelliteDirectActivity.this,ClsMainActivitySatelliteDirect.class);
    b = new Bundle();
    setContentView(R.layout.initial_splash_screen);
    boolean bCheckInternetConnectivity = checkInternetConnection();
    if(!bCheckInternetConnectivity) 
    {
        Toast.makeText(this, "Please ensure that you have a internet connectivity", Toast.LENGTH_SHORT);
        finish();
    }
    else 
    {
        new GetCountryInformation().execute();

    }
    startActivity(intent);
        finish();
}
private boolean checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    // test for connection
    if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        Log.v("ERROR_LOG", "Internet Connection Not Present");
        return false;
    }
}
public class GetCountryInformation extends AsyncTask<String, Void,String> {


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

    }

   /*   protected void onProgressUpdate(Integer... progress) {
        //setProgressPercent(progress[0]);
    }*/
    @Override
    protected String doInBackground(String... params) {
        JSONObject mJsonObject = ClsGetJsonFunction.getJSONfromURL("http://www.sachdevbros.com/sdandroid/videos/country.php");
        String [] sCountryNames = null;
        String [] sCountryCid = null ;
        try  
        {
        JSONArray mJsonArray = mJsonObject.getJSONArray("results");
        sCountryNames= new String[mJsonArray.length()];
        sCountryCid= new String[mJsonArray.length()];
        for(int icount = 0 ; icount <mJsonArray.length()-1; icount++) 
        {
            JSONObject mJsonObject2 = mJsonArray.getJSONObject(icount);
            sCountryNames [icount] = mJsonObject2.getString("country");
            sCountryCid[icount] = mJsonObject2.getString("cid");
        //  Log.v("JSON", ClsGlobalConstants.sGLB_sCountryNames[icount]+ClsGlobalConstants.sGLB_sCountryCid[icount]);
        }

        }catch(JSONException je) 
        {
            Log.v("ERROR_TAG", ""+je);
        }
        b.putStringArray("cou", sCountryNames);
        b.putStringArray("cid", sCountryCid);

        intent.putExtras(b);

        return null;
    }


    protected void onPostExecute(Void... aa) {
       super.onPostExecute(null);
        //  showDialog("Downloaded " + result + " bytes");

     }
}

and receiving this as

        final String country[] =this.getIntent().getStringArrayExtra("cou");
    String cid[] =this.getIntent().getStringArrayExtra("cid");
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • Are you getting a NullPointerException? If so, please post the logcat – Parth Doshi Dec 21 '11 at 04:25
  • getting no error i am passing this array to to alertdialog to populate list. but list is always empty i tried debugging and at that point the array is null. – Sunny Dec 21 '11 at 04:28
  • try passing the array in the onPostExecute method and not the doInBackGround method – Parth Doshi Dec 21 '11 at 04:32
  • Your doInBackground method has return type as `String` then why you returning null ? – Parth Doshi Dec 21 '11 at 04:35
  • How did you find that the array was empty? Did you log the values? Have you tried logging the JSONObject returned by `ClsGetJsonFunction.getJSONfromURL("http://www.sachdevbros.com/sdandroid/videos/country.php")`. Maybe that itself is empty. – Arnab Chakraborty Dec 21 '11 at 04:43
  • put startActivity(intent); finish(); these two lines on AsyncTask's postExecute() and run again let me know what happen.. – user370305 Dec 21 '11 at 05:00
  • Thanks every one problem getsolved the probvlem is in async task and also i execute the startActivity in onPostExecute() also Override the onPostExecute. – Sunny Dec 21 '11 at 05:10
  • this post help me http://stackoverflow.com/questions/4494720/asynctask-onpostexecute-never-gets-called – Sunny Dec 21 '11 at 05:12

2 Answers2

6

This is because, you are starting other activity immediately after start the AsyncTask, It cause bundle empty, So if possible put your staring other activity code in AsyncTask's postExecute().. Then you can get values in other activity..

For this pass your activity reference in AsyncTask and using that reference call startActivity()..

protected void onPostExecute(Void... aa) {
       super.onPostExecute(null);
        //  showDialog("Downloaded " + result + " bytes");
       mContext.startActivity(intent);
       mContext.finish();
     }
user370305
  • 108,599
  • 23
  • 164
  • 151
1

You passing the value in a bundle

 b.putStringArray("cou", sCountryNames);
 b.putStringArray("cid", sCountryCid);
 intent.putExtras(b);

So you should get the bundle value like this.

 Bundle b = getIntent().getExtras();
 String  mJsonvalue[] = b.getStringArrayList("cou");

I think this will solve your problem.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
droid kid
  • 7,569
  • 2
  • 32
  • 37