Reputation: 1607
I am getting a user input from two text fields and storing them into a hash map. I have checked to see if the values are being inserted correctly to the hash map. and they are. once I try to pass it using Intent.putExtra, I am getting this error.
Unable to start activity ComponentInfo{com.example.test.retrofit/com.example.test.retrofit.SearchActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
at com.example.abdulhakim.retrofit.SearchActivity.onCreate(SearchActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
here is my code in MainActivity:
Map<String, String> data = new HashMap<String,String>();
input1 = (EditText) findViewById(R.id.term);
input2 = (EditText) findViewById(R.id.column);
ImageView =(ImageButton)findViewById(R.id.search);
ImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SearchActivity.class);
k=input1.getText().toString();
t= input2.getText().toString();
data.put("key",k);
data.put("term",t);
Log.v("HashMapTest KEY", data.get("key"));
Log.v("HashMapTest TERM", data.get("term"));
intent.putExtra("hash",data);
startActivity(intent);
}
});
and here is my second activity:
Intent intent = getIntent();
HashMap<String, String> data = (HashMap<String, String>)intent.getSerializableExtra("hash");
if (data.isEmpty()) {
Toast.makeText(getApplicationContext(), "nope! I didnt get the hashmap", Toast.LENGTH_LONG).show();
return;
}
Upvotes: 0
Views: 2771
Reputation: 23881
try this: in your First activity:
HashMap<String, String> data = new HashMap<String,String>(); //initialize properly
Intent intent = new Intent(MainActivity.this, SearchActivity.class);
intent.putExtra("hashMap", data);
startActivity(intent);
Now in second Activity Get data using:
Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) intent.getSerializableExtra("hashMap");
String key = hashMap.get("key");
Sting term = hashMap.get("term");
problem lies in your Second activity
you are trying to cast String
to a Hashmap
Upvotes: 0
Reputation: 756
This is pretty simple, All Collections objects implement Serializable (sp?) interface which means they can be passed as Extras inside Intent
Use putExtra(String key, Serializable obj) to insert the HashMap and on the other Activity use getIntent().getSerializableExtra(String key), You will need to Cast the return value as a HashMap though.
Upvotes: 0
Reputation: 290
use this
intent.putExtra("hash",data.toString());
OR
intent.putSerializableExtra("hash",data)
Upvotes: 1