开发者

showing "null" when value comes from another class of java android

i got the data from server when i go to use that value comes from server in another class it display null instead of actual value comes from server...i have two class activity2.java and notepadv3.java data comes from server in activity2.java and when i use that data it display null ...i am going to pasted both of the class...pls solve my problem why it shows null?????

package cabs.h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Notepadv3 extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private NotesDbAdapter mDbHelper;
private Long mRowId;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listonruntime);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());

}
private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);
     String s=NotesDbAdapter.KEY_TITLE;
    Log.i("saurabh trivedi log cat,,,,,,,,,",s);
    final String PREFS_NAME1 = "PrefSettings";

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_BODY,NotesDbAdapter.KEY_TITLE};
  // String[] too = new String[]{NotesDbAdapter.KEY_TITLE};
    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.toptext,R.id.middletext};

   SimpleCursorAdapter notes =   
                                new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);

setListAdapter(notes);

}




@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteNote(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

  @Override
protected void onSaveInstanceState(Bundle outState) 
{
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();

}

**

**//sss variabe value comes null when display it
     Activity2 activity=new Activity2();
     String sss=activity.resp1;
   //String sss = cabbookingapplication.resp;
    String city = cabbookingapplication.Selection;
    private void saveState() {

        String title =("FROM LOC::"+sss);
          String body = ("TO LOC::"+city);**
            if (mRowId == null) {**
            long id = mDbHelper.createNote(title, body);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateNote(mRowId, title, body);


        }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this,FeedActivity.class);
       startActivity(i);
    //   i.putExtra(NotesDbAdapter.KEY_ROWID, id);
     //  startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
        super.onActivityResult(requestCode, resultCode, intent);

        fillData();

    }

}

and Activity2.java class code is

package cabs.h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Activity2 extends Activity {
public  static String resp1;
   @Override
    public  void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState);


                     try {

                      String tURL="http://qrrency.com/mobile/j2me/cab/CabRequestStatus.php?requestid=130";
                      URL u开发者_JS百科rl = new URL("http://qrrency.com/mobile/j2me/cab/CabRequestStatus.php?requestid=130");
                      Log.i("Line----saurabh trivedi-----", tURL);
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        int m=0;
                        StringBuffer buffer=new StringBuffer();
                    String str1 = " ";
                        while ((m=in.read())!=-1) 
                        {
                                   **buffer.append((char)m);
                                    str1=str1+(char)m;
                                 cabbookingapplication.resp =str1;
                                 resp1=cabbookingapplication.resp;
                           Log.i("Line----saurabh trivedi---,,,--",resp1);**

                        }



                       in.close();



                        } catch (MalformedURLException e)
                        {
                        } catch (IOException e) 
                        {

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

   }      


I think you should not extend Activity2.java from Activity if you don't need an UI in it. The onCreate method of Activity2 is not executed, if you instaniate Activity2.java by the "new" keyword as you do in Notepadv3.java. onCreate is not a contructor. That's why your static variable "resp1" is null, because it is never set.

Remove "extends Activity" from your Activity2 and move the code from onCreate into a static function returning your data, which you can call in Notepadv3.java.

class Activity2 {
  public static String getData() {
    String data;
    // you code for retrieving data
    return data;
  }
}

In your Notepadv3 class call

String sss = Activity2.getData();

to get your data. An Activity is a UI class from Android to extend from. You should never instantiate a subclass of it by "new" keyword. The system is creating an instance for you if you i.e. start an intent an then onCreate is called by the system during the lifecycle of the activity.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜