【问题标题】:Getting always null value async task始终获取空值异步任务
【发布时间】:2011-12-21 04:18:27
【问题描述】:

我将值传递给另一个活动,但总是得到空值

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

【问题讨论】:

  • 您是否收到 NullPointerException?如果是这样,请发布 logcat
  • 没有错误我将这个数组传递给 alertdialog 以填充列表。但列表总是空的,我尝试调试,此时数组为空。
  • 尝试在 onPostExecute 方法而不是 doInBackGround 方法中传递数组
  • 您的 doInBackground 方法的返回类型为 String 那么为什么您返回 null ?
  • 怎么发现数组是空的?您是否记录了这些值?您是否尝试过记录ClsGetJsonFunction.getJSONfromURL("http://www.sachdevbros.com/sdandroid/videos/country.php") 返回的 JSONObject。也许它本身就是空的。

标签: android android-intent android-asynctask bundle


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你在一个包中传递值

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

    所以你应该得到这样的捆绑价值。

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

    我想这会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2015-12-04
      • 2013-04-16
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多