始终获取空值异步任务

发布于 2024-12-22 11:45:36 字数 3041 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-12-29 11:45:36

这是因为,您在启动 AsyncTask 后立即启动其他 Activity,这会导致包为空,因此如果可能,请将您关注的其他 Activity 代码放在 AsyncTask 的 postExecute() 中。然后您可以获取其他活动中的值。

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

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

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();
     }
大姐,你呐 2024-12-29 11:45:36

您在捆绑包中传递值

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

因此您应该像这样获得捆绑包值。

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

我想这会解决你的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文