开发者

display "null" when transfer value of "str1" to other class

public class ServerResponce extends ListActivity {
    private ArrayList<listobj> tobj = new ArrayList<listobj>();
    **static String str1;**
     P开发者_运维问答ickUpLocation pickup=new PickUpLocation();
  String pickuplocid=  pickup.locationid;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new MyTask().execute();

}

  private class MyTask extends AsyncTask<Void, Void, Void>
  {
          private ProgressDialog progressDialog;
          protected void onPreExecute() {
                  progressDialog = ProgressDialog.show(ServerResponce.this,
                                    "", "Loading. Please wait...", true);
          }
          @Override
          protected Void doInBackground(Void... arg0) {
                  try {





                URL  tURL=  new URL("http://qrrency.com/mobile/j2me/cab/CabBooking.php?userid=1&from=home&to=office&datetime=&cabservicetype=1&cabtype=1&cabfeatures=AC&locationid="+pickuplocid+"");

                  BufferedReader inn = new BufferedReader(new InputStreamReader(tURL.openStream()));
                    int p=0;

                    StringBuffer buffer1=new StringBuffer();

                    while ((p=inn.read())!=-1) 
                    {
                            buffer1.append((char)p);
                            str1=str1+(char)p;

                    } Log.i("Line----saurabh trivedi-ddddd----", str1);

                   inn.close();

I make object of class and transfer the str1 value to other class ServerResponce

servresp=new ServerResponce();
String serv= servresp.str1;
Log.i("tag",serv);

serv is showing "null" why??


When your String is static then you dont need to create Objetc of that class. For reference in 2nd Activity you could use.

ServerResponse.str1;

in your second Activity


ListActivity is not a class that's meant to be instantiated by you, it's a framework thing that should be handled by Android itself.

onCreate() is not getting called when you instantiate the class, that's Android's job, when an activity is started via an intent. So MyTask is not created nor executed.

Moreover, even if you executed MyTask in the constructor, it wouldn't work either, because with:

ServerResponce servresp=new ServerResponce();
String serv= servresp.str1;
Log.i("tag",serv);

you're just starting the task (which will take some time to be executed!) and immediately querying the result string. You have to use the onPostExecute() callback to be sure that you have a value.

(aren't you going a bit too fast?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜