How to fix InvocationTargetException when sending hashmap to other activity - java

I'm trying to send HashMap to start new activity with startActivity(intent).
I followed the answer from this link:
How to send hashmap value to another activity using an intent
But I still get error
Of course, i tried to send String
intent.putExtra("test","some String");
startActivity(intent);
And it worked
My code:
HashMap<String,Diner> dinersOrdersHasMap = new HashMap<String,Diner>();
FillHashMap(); // Fill the HashMap with data
Intent intent = new Intent(this, BillForm.class);
intent.putExtra("dinersOrderHashMap",dinersOrdersHasMap);
startActivity(intent);
The exception:
"Could not execute method for android:onClick", e);
e.detailMessage = "Parcelable encountered IOException writing serializable
object (name = com.example.myfirstapp.Diner)
When i call
startActivity(intent);

is your Diner class Serializable or Parcelable ?
Could you post here your Diner class ?

Related

webview load url from input text in another class [duplicate]

This question already has answers here:
Sending data back to the Main Activity in Android
(13 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
i have two class, MainActivity and InputAddress, how to load url from class InputAddress
view.loadUrl("here, im confused");
send data from inputAddrss,
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("url", YOUR_EDIT_TEXT.getText().toString());
startActivity(intent);
receive data in MainActivity,
String s = getIntent().getStringExtra("url");
then load into webview
view.loadUrl(s);
You can add a public static variable like this:
public class InputAddress {
public static String address = "abc";
}
then you can access the address variable in MainActivity like this:
public class MainActivity {
// ...
public void onCreate(....) {
Log.d("TAG", InputAddress.address);
}
}
Hi Deki Kurnia Hadi Permana,
There are lots of way to transfer data from one class to another class, But for the activity no need to do anything they provide functionality to transfer data one to another activity using "Intent" data,
Following is the code where you can send and access data in another activity.
FirstActivity.class
Intent callIntent=new Intent(FirstActivity.this,SecondActivity.class);
callIntent.putExtra("urlToLaunch","post url here");
startActivity(callIntent);
SecondActivity.class
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String urlToLaunch=bundle.getString("urlToLaunch");
// set In webbrowser
}

Android Passing big data on preview screen before user loads MainActivity with HashMap TransactionTooLargeException

Hi everyone i am having a problem here so here is what i am trying to achieve.
Achieve:
Trying to create a Map and share it between activitys! And use it in MainActivity.
Reason:
Trying to avoid the big load of the data on the MainThread by loading the data in my SplashActivity into a Map and sending the loaded data into my MainActivty
so i can use it there to check it from user Input if it matches.
Problem:
Problem is that the data i am trying to load is this one:
http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt
Which is a big wordlist and it takes a while to load in background. It has to be fast thats why i am trying to load it in Map before the MainActivity Load and then passing it when its done loading btw user is waiting for SplashScreen while load to the Map.
Getting error that the data i am trying to send true Bundle, Intent is too large.
Anyone having thoughs how i can pass the loaded Map into the MainActivity?
Yes i know i can use AsyncTask as the background thread...
Yes i know it can be done with Serialize but please show some examples of your thoughts.
Thanks for everyone who is trying to help!
ThePreviewActivity code:
public class PreviewActivity extends AppCompatActivity {
private Scanner scanner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Map<Character, ArrayList<String>> charCount = new HashMap<Character, ArrayList<String>>();
//get Assets
AssetManager am = this.getAssets();
//create input stream
InputStream is = null;
try {
//open the word txt
is = am.open("words.txt");
} catch (IOException e) {
e.printStackTrace();
}
if (is != null) {
scanner = new Scanner(is);
}
while (scanner.hasNext()) {
char firstChar = scanner.next().charAt(0);
ArrayList<String> list;
if (charCount.containsKey(firstChar)) {
list = charCount.get(firstChar);
} else {
list = new ArrayList<>();
}
list.add(scanner.next());
charCount.put(firstChar, list);
}
Intent intent = new Intent(this, MainActivity.class);
Bundle extras = new Bundle();
extras.putSerializable("HashMap", (Serializable) charCount);
intent.putExtras(extras);
startActivity(intent);
finish();
}
}
MainActivity.java code:
Bundle bundle = this.getIntent().getExtras();
if(bundle!=null) {
charCount = (Map<Character, ArrayList<String>>) bundle.getSerializable("HashMap");
}
Error i am getting trying to pass it with a Bundle: Caused by: android.os.TransactionTooLargeException: data parcel size 3560868 bytes
First, please move all this I/O-and-parsing code to a background thread.
In terms of your TransactionTooLargeException, you simply cannot pass that much data around via Intent objects using startActivity(). You could:
Store this data in a singleton, particularly since it will not be changing (presumably), or
Wrap the data in a ContentProvider, and have other activities use a ContentResolver to work with that provider

How can I receive send and recieve multiple data with an implicit intent?

I am sending an implicit intent from one activity to another and I want to send some String variables. I'm doing this as an implicit intent as I wish to use activity B in multiple projects, it's part of a library project.
I have got the intent working just fine but the extra data is not being read in at activity B.
Here's my code in Activity A to create the intent:
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
intent.putExtra(RESULTS_SAVENAME, saveName);//String
intent.putExtra(RESULTS_BODY, body);//String
intent.putExtra(WITH_SAVE_BUTTON, withSaveButton);//Boolean
startActivity(intent);
And in Activity B:
Bundle extras = getIntent().getExtras();
Boolean withSaveButton = true;
if(extras != null){
emailSubject = extras.getString("EMAIL_SUBJECT");
resultsSaveName = extras.getString("RESULTS_SAVENAME");
resultsBody = extras.getString("RESULTS_BODY");
withSaveButton = extras.getBoolean("WITH_SAVE_BUTTON", true);
}
This doesn't work and the String variables are null.
I have also tried to do this in the way an explicit intent recieves the data:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("EMAIL_SUBJECT");
resultsSaveName = intent.getStringExtra("RESULTS_SAVENAME");
resultsBody = intent.getStringExtra("RESULTS_BODY");
Boolean withSaveButton = intent.getBooleanExtra("WITH_SAVE_BUTTON", true);
But this doesn't work either.
The only way I can get it to work is if I reference the sending Activity in getStringExtra but I can't do this as it's supposed to be a library class.
Can anyone shed any light on this? Or am I going about this in the wrong way?
I assume your code like this:
final static String EMAIL_SUBJECT = "email_subject";
// RESULTS_SAVENAME and others are the same style.
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
startActivity(intent);
then in your another activity:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("email_subject");
// or this
// emailSubject = intent.getStringExtra(EMAIL_SUBJECT);
Please make use of the key , it should be same in both the activities. I clearly see a difference in naming convention of the key name in both the activities.If key name is same, no way it data should not pass.

Getting launcher activity class name

Is there a way to get the class name of the activity that launched the Intent without putting extras to the Intent?
I use the following code to launch other activities.
Intent intent = new Intent(CallerClass.this, TargetClass.class);
startActivity(intent);
Can I get the caller class' name in the target class?
One way you could do it is by using startActivityForResult() instead of startActivity(Intent) and then have the second activity use getCallingActivity() to get the CallerClass.
use startActivityForResult then you can retrieve activity caller by : getCallingActivity().getClassName()
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();

How can I start a new android activity using class name in a string?

I'm having a problem with an android application that I'm working on.
My application has several sections and the next screen that loads is based on a string. So, screen 1 of section 1 would be, S1S1.
My question is, how can I start an activity based on a string. I have S1S1 saved in a string, let us call it next activity. Rather than having to type S1S1.class, I need it to come from the string. I've tried everything I can think of and google hasn't helped much.
Some things I've tried are
Intent myIntent = new Intent(nextactivity);
Intent myIntent = new Intent(v.getContext(), getClass().getName().valueOf(nextactivity));
Intent myIntent = new Intent(v.getContext(), Class.forName(nextactivity));
and tried running with
startActivityForResult(myIntent, 0);
but nothing seems to work. Any ideas?
Here is a code by which you can start activity using the name of the activity
String activityToStart = "com.example.MainActivity";
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException ignored) {
}
EDIT
Here class name will be full name of the class with the package name.
For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.
An even better way (and one that is used in the system to launch Browser.apk along with other apps that aren't bundled with AOSP):
Intent intent = new Intent();
intent.setClassName("com.android.browser","com.android.BrowserActivity");
context.startActivity(intent);
Alternatively, if you want to check that you can start the Activity from the command line, you can do something like this from your shell:
adb shell
am start com.android.browser/.BrowserActivity
I am not aware of solution but i have an alternative.. the way similar to div hide and show in web pages.
if your s1s1 is to loaded low content have them in a linearlayout and keep their visibility gone on loading form s1. when you click on s1 to reach s1s1 hide s1 and set the params of visibility to "visible".
By doing this you can avoid creating a separate activity and this way is also easy to navigate back.
Use Enums!
public enum SectionActivity {
S1S1(MyS1Activity.class),
S1S2(S2Activity.class);
private Class<? extends Activity> activityClass;
private SectionActivity(Class<? extends Activity> clazz) {
this.activityClass = clazz;
}
public Class<? extends Activity> getActivity {
return activityClass;
}
}
Then somewhere in your code:
SectionActivity act = SectionActivity.valueOf(string);
Intent intent = new Intent(this, act.getActivity());
startActivity(intent);

Categories

Resources