Passing HashMap to other activity throws class cast Exception - java

I want to pass a HashMap to other Activity my hash map is
HashMap<Integer,float[][]> table;
I use this code to send it to other activity
Intent i = new Intent(this, LineChartActivity.class);
i.putExtra("map",table);
startActivity(i);
when I receive it
Intent intent = getIntent();
table = (HashMap<Integer, float[][]>) intent.getSerializableExtra("map");
the VERY weird thing is that this code works very well in Android 5 and higher but in the previous versions Eg 4.4.4 ,4.1.1 it throw
java.lang.ClassCastException: java.lang.Object[] cannot be cast to
float[][]
Does someone face this problem before ? please I need any solution

Related

The app back to previous page when I attempt to fetch data from another activity

I have 1 adapter file, contain following code:
//When user click detailBtn, it will pass the data to EventDetail page.
holder.detailBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(holder.detailBtn.getContext(), EventDetail.class);
intent.putExtra("eventName", datalist.get(position).getEventName());
intent.putExtra("eventDate", datalist.get(position).getEventDate());
intent.putExtra("eventVenue", datalist.get(position).getEventVenue());
intent.putExtra("eventDesc", datalist.get(position).getEventDesc());
intent.putExtra("eventRole", datalist.get(position).getEventRoles());
intent.putExtra("eventCreator", datalist.get(position).getEventCreator());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
holder.detailBtn.getContext().startActivity(intent);
}
});
The main problem occur here, following code is caused the problem::
So, when I try to access the field "eventRole" and put it in ArrayAdapter in EventDetail java file using following code:
String[] roles = (String[]) getIntent().getStringArrayExtra("eventRole");
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.option_item, roles);
The app jump back to previous page :(
Can anyone help me to look into why it return back to previous activity when I try to access it? I can't find any clue on the logcat it shows to me...
The "eventRole" array is stored as Map Structure in Firestore.
Are you sure that your getEventRoles() method return String[] ? As far as I understood, you have casted getIntent().getStringArrayExtra("eventRole"); to String[] even if getStringArrayExtra() is returning String[], Why ?
I think the problem is in your getEventRoles() method, it might not be returning String[] and you are trying to cast that incompatible type to String[] which is throwing ClassCastException.

How to send an object from imported package to another Activty

I've made an activity (A) that recovers a List<Event> from Google Calendar API in an AsyncTask, and then sends it from (A) to (B). The list isn't empty when putting it into the bundle:
bundle.putSerializable(KEY_EVENTS_LIST, (Serializable) items);
but it's null when I recover it in (B)
Bundle b = getIntent().getExtras();
eventList = (List<Event>) b.getSerializable(MainActivity.KEY_EVENTS_LIST);
I don't know which other way I could send it from (A) to (B), or if I could send the List directly to B from the AsyncTask.
You can send serialized array list (ArrayList) using intent from Activity A to Activity B
Intent intent = new Intent(context, B.class);
intent.putExtra("list", serializedArrayListObject);
activity.startActivity(intent);
In Activity B in onCreate method
ArrayList<Event> list=getIntent().getSerializableExtra("list");
to get list from intent
The WorkAround you can do to tackle this creates an Event Class in your project with all the attributes of Event(another package) class and make your Event class a paracalable.
Before setting list to bundle convert it to your list and pass this list.
This is not a good solution but up to my knowledge it will work perfectly.
Hope this will help you.
If you want to save the List in background while sending it to the other activity, you can use sharedPreferences for that as shown below:-
1) You cannot directly save an arrayList in SharedPreferences, so you need to convert it to a set first by:-
Set<String> set = new HashSet<String>();
set.addAll(Your_ArrayList);
2) Save it using sharedPreferences:-
SharedPreferences.Editor editor = getSharedPreferences(MY_PREF_STRING, MODE_PRIVATE).edit();
editor.putStringSet("Key", set);
editor.apply();
where MY_PREF_STRING is declared globally as public static final String MY_PREF_STRING = "MY_PREF";
Retrieve this using
1) In another activity where you want to retrieve this just paste the code given below:-
SharedPreferences preferences = getSharedPreferences(MY_PREF, MODE_PRIVATE);
Set<String> set2 = new HashSet<String>();
set2 = preferences.getStringSet("Key", Collections.singleton("0"));
2) You can again convert this to arrayList using:-
ArrayList<String> array = new ArrayList<String>();
array.addAll(set2);

Unable to retrieve data of type Paint.Cap from a bundle

I have looked everywhere for this and I couldn't find any answer.
I am sending data over an intent like this.
MainActivity.java
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key",Paint.Cap.SQUARE);
startActivity(intent);
My issue is in retrieving it, I don't know which get method to use and I couldn't find any source online that tells me.
SecondActivity.java
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
if(extras.containsKey("key"))
{
Paint.Cap shape = //which method to use here?
}
}
If it's something so silly please tell me, I am still a beginner and tried my best to find it on my own.
Thanks.
The key is that Paint.Cap is an enum and so it implements the interface Serializable.
This means that you called the version of putExtra that takes a Serializable.
To get it out again you can do
Paint.Cap shape = (Paint.Cap) extras.getSerializable("key");

How can I open an Activity using the name as string via intent?

I want to use String variable containig the name of an activity, and i want to open the activity via in intent.
For example:
next = "foo.class";
Intent baslat = new Intent(this,next);
"next" is my value. I think using variable is impossible because eclipse don't let me use two arguments.
How can I solve this problem?
Edit: I am trying to go to "foo.class"
Edit: I solve the problem, You are all so nice and pretty :D, kisses for all, thank you very much!
OK, use the method Class.forName()
String myClass = "foo.class";
Intent myIntent = new Intent(getApplicationContext(), Class.forName(myClass));
startActivity(myIntent );
There is a method Intent.putExtra(). You can use this method to add extra variables inside your intent object.
String next = "foo.class";
Intent baslat = new Intent();
baslat.putExtra("my_tag", next);
Intent.putExtra() is what you're looking for.
next = "foo.class";
Intent baslat = new Intent(/*intent action goes here*/);
basalt.putExtra(/*data name*/, next);
If foo.class is where you're headed, then use Intent(Context packageContext, Class cls):
Intent baslat = new Intent(this, foo.class);
startActivity(basalt);

Launching intent from a class outside an activity

I've got two activities, one of them is called MyActivity. I want both of them to be able to use a function located in a class othat we may call MyClass. In MyClass, I try to use an intent to launch the activity AnotherActivity. Since the constructor takes a context as parameter, I simply tried to store a context from the activity in the constructor, and then use it when I try to create my intent.
class MyClass {
private Context cxt;
MyClass(Context cxt) {
this.cxt = cxt;
}
startIntent() {
Intent intent = new Intent(cxt, AnotherActivity.class);
startActivity(intent); // this line throws a NullPointerException
}
}
The code in MyActivity to use the class is shown below:
myClassObject = new MyClass(MyActivity.this);
myClassObject.startIntent();
However, even thought none of the arguments are null (checked that with a simple if-statement), intent seems to be null and a NullPointerException is thrown. Why does it not work, and what can I do to solve the problem?
I'm quite new to Android and Java development, so please explain it as basic as you can.
cxt.startActivity(new Intent(cxt, AnotherActivity.class));
and to be sure that it's intent is NULL, and not something internal in startActivity method, you can add some checks, i.e.
Intent intent = new Intent(cxt, AnotherActivity.class);
Log.d(toString(), "intent = " + intent.toString());
cxt.startActivity(intent);
I've used almost identical code in my applications and it's worked fine.
I suspect there's something else going on that's in code you haven't shown us; I suspect there's some cut-and-paste issues --- e.g. what are you calling startActivity() on in MyClass?

Categories