始终获得空值异步任务

时间:2011-12-21 04:18:27

标签: android android-intent android-asynctask bundle

我将值传递给另一个活动,但始终为null值

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

     }
}

并将其作为

接收
        final String country[] =this.getIntent().getStringArrayExtra("cou");
    String cid[] =this.getIntent().getStringArrayExtra("cid");

2 个答案:

答案 0 :(得分:6)

这是因为,您在启动AsyncTask后立即启动其他活动,这会导致捆绑为空,所以如果可能的话,请将您的其他活动代码放在AsyncTask's postExecute()中..然后您就可以了在其他活动中获取价值..

为此,在AsyncTask中传递您的活动参考并使用该参考调用startActivity() ..

protected void onPostExecute(Void... aa) {
       super.onPostExecute(null);
        //  showDialog("Downloaded " + result + " bytes");
       mContext.startActivity(intent);
       mContext.finish();
     }

答案 1 :(得分:1)

您在包中传递值

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

所以你应该得到这样的bundle值。

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

我认为这可以解决您的问题。