2

I want to transfer the object from one activity to another .

in my first class I have put the following

Intent intenttt ;

Intent intentttt.putExtra("user_searchh", cur.toString());

here the cur is the object of Cursor. I want to transfer it to second class.

in my second class I have put following

Cursor c = (Cursor) getIntent().getSerializableExtra("user_searchh");

I tried to to run both the classes without the above code , it works properly .

But, when I place the above code , it prompts the force to close error.

In DDMS there is error like ... NulpointerException ... DirectCursorDriver.... etc..

I take teke reference from How to pass an object from one activity to another on Android

having 50 votes.

help me if possible .

thanks ...

Community
  • 1
  • 1
Munjal Upadhyay
  • 189
  • 2
  • 4
  • 13
  • 1
    You are trying to pass a cursor which has been converted to String and then casting it back to a cursor. I think it is wrong in so many different levels. You need to do some serious redesigning. – Shubhayu Apr 14 '12 at 05:48
  • then what to do if , I want to transfer Cursor object from one one activity to another ? – Munjal Upadhyay Apr 14 '12 at 05:55
  • Do you want from Cursor will more help clear us what you want? – Herry Apr 14 '12 at 05:57
  • now , I encounter a problem that , if I add Intent intentttt.putExtra("user_searchh", cur.toString()); in first class , there is an error like FATALEXCEPTION , NullPointerException . the class don't let me add even the putExtra() method in the first class... – Munjal Upadhyay Apr 14 '12 at 06:06
  • You have make one `Serializable` Class in that class you can put your Cursor to pass to other Activity. – Herry Apr 14 '12 at 06:09
  • thanks for your effort , I try all the suggession from the google ,, but it don't work there is a error NullPointerException ... – Munjal Upadhyay Apr 14 '12 at 17:34

5 Answers5

1

I got it ....

first of all ,, when you translate the object to string .. you can never cast it back to the object..

secondly , rather to transfer object from one Activity to another ,, it is preferable to transfer strings from one Activity to second activity .. and then compute the stuff at the second activity ...

while transfering Strings from one Activity to another Activity I made following two mistakes...

1) the first mistake I am making is ....

I use two intent object .. e.g.

Intent i = new Intent(user_search2.this,rest_name_share.class);

     Intent i1= new Intent();
    i1.putExtra("restaurant_email", email_of_restaurant);

     startActivity(i); 
             startActivity(i1);

rather you should write like below

Intent i = new Intent(user_search2.this,rest_name_share.class);

    i.putExtra("restaurant_email", email_of_restaurant);  // here email_of_restaurant is a String object ..
    // you can aslo put more  than one strings...
     startActivity(i); 

2) second mistake is that I call the getStringExtra() at the class level .

It should be called in the onCreate() method

the stuff to be called in onCreate() is

Intent intent = getIntent();

String email_of_restaurant = intent.getStringExtra("restaurant_detail");

thanks to all ,,....

Munjal Upadhyay
  • 189
  • 2
  • 4
  • 13
1

That's not how Serializable works, you are right that at a fundamental level the object gets converted to a String, but it's much more complicated than just calling obj.toString();

The only objects that you can pass through an Intent are ones that implement the Serializable interface.

So if there is information in the Cursor that you need to pass on, take that out and wrap it in some kind of Serializable object.

Mike Ounsworth
  • 2,444
  • 21
  • 28
0

OMG. This is quite a little disaster area of a question, huh?

The OP's problem is simple: he is putting a String into an Intent and then trying to retrieve a Serializable. A String is, certainly, Serializable, but the cast to a Cursor isn't going to work.

If one were to attempt to be helpful, instead of just correct, one could point out that, in general, this just isn't going to work. Attempting to Parcel or Serialize a cursor -- an object that represents a connection to a database -- is all but impossible. Consider, for a moment, what it would mean to marshal an entire cursor's data into an intent. But then, I did say "all but" because, actually, using some kind of Binder magic, Android does support Cross-process Cursors (I'd include the link, but SO forbids it). But, no: you can't put a Cursor into an Intent. At all. Ever.

Finally, though, about 3 answers ago, someone should have stopped the insanity and asked, "WTF, Dude??? What are you trying to do??" Here are some ways to accomplish whatever the OP is trying to do:

  • Pull the data that you need into a model object tree and pass a reference to the it
  • Re-run the query (this was suggested above, but with no supporting reason)
  • Put the cursor into a global and refer to it from both Activities.
G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
-1

You have put String Object in this code

So try below code will work.

  Intent intentttt.putExtra("user_searchh", cur.toString()); 

 String str=getIntent().getStringExtra("user_searchh");

for Pass Cursor You need to do Something Like this make one class like below.

public class MyCursor implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
Cursor mCursor;

public void setCursor(Cursor paramCursor){
    mCursor=paramCursor;
}

public Cursor getCursor(){
    return this.mCursor;
  }
}

Now before put Object in to PutExtra initialize it with below code

   MyCursor mObject=new MyCursor();
    //You can set your Cursor in Below code
   mObject.setCursor(mCursor);
   mIntent.putExtra("mCursor",mObject );

Now in other Activity you can get Cursor by below code.

   MyCursor mGetCursor;
  mGetCursor=(MyCursor) getIntent().getSerializableExtra("mCursor");
    if(mGetCursor!=null){
        mGetCursor.getCursor();
    }
Herry
  • 7,037
  • 7
  • 50
  • 80
-1
//For passing :
intent.putExtra("MyKey", YourObj);  // From First Activity

// to retrieve object in second Activity
Object obj = getIntent().getSerializableExtra("MyKey"); //In Second Activity

Now you can Convert the Object. Hope this Helps You.

Bhavin
  • 6,020
  • 4
  • 24
  • 26