始终获取空值异步任务
我正在将值传递给另一个活动,但总是得到空值
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为,您在启动 AsyncTask 后立即启动其他 Activity,这会导致包为空,因此如果可能,请将您关注的其他 Activity 代码放在
AsyncTask 的 postExecute()
中。然后您可以获取其他活动中的值。为此,请在 AsyncTask 中传递您的活动引用并使用该引用调用
startActivity()
..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()
..您在捆绑包中传递值
因此您应该像这样获得捆绑包值。
我想这会解决你的问题。
You passing the value in a bundle
So you should get the bundle value like this.
I think this will solve your problem.