I am trying to get the entered data from an activity. From my main screen, I kick off the activity like this:
Intent myIntent = new Intent(this, ContactInfo.class);
startActivityForResult(myIntent, AppState.ACTIVITY_CONTACT_INFO);
In the activity, upon the user tapping the Save button, I fire off the following:
Intent intent = new Intent();
TextView tvName = (TextView) findViewById(R.id.txtContactName);
intent.putExtra("Name", tvName.getText());
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
In the original activity, I catch the onActivityResult event like this:
String contactName = (String) data.getExtras().get("Name");
However, this line blows up with java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String. I've also tried getStringExtra with same results.
What am I missing?
TextView.getText() doesn't return a string, but rather the SpannableString you see. Use getText().toString() instead.
Had a similar problem, where the error appear, android.text.SpannableString cannot be cast to java.lang.String
It was because getText() needs to have a 'toString()' at the end. Without this 'toString()', it will crash on Android 4.x, but will work on Android 2.x.
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView viewTxt; // Continue and complete this yourself.
String tmpStr = (String) viewTxt.getText().toString();
Related
Please am designing an android app, i want to send (5)String values from one activity to another activity to use in different TextViews, i have tried virtually all the code i could find online on the topic, but i keep getting just one value(the last value i send in the putExtra()). please i am new to Android Studio and will appreciate every help.
I have used the putExtra() to send one data to another activity and it worked perfectly, while trying to do the same with multiple data i keep getting just one of the data sent.
I have also tried using a bundle object, to receive the data from the other(recieving) activity.
I expect getting all this data ( intent.putExtra("surname", "Jerry").
intent.putExtra("middlename", "chris"). intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone
this is my code;
//in the firstActivity
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sFirstname = firstname.getText().toString();
String sLastname = lastname.getText().toString();
Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);
intent.putExtra("surname" ,sFirstname);
intent.putExtra("lastname", sLastname);
startActivity(intent);
}
});
//And In the second Activity
firstname = findViewById(R.id.firstname);
lastname = findViewById(R.id.firstname);
Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();
String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);
Try this.
In First Activity:
String sFirstname = "Tope";
String sLastname = "Adebodun";
Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);
Then in your second activity, do this in the onCreate method:
Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");
I would prefer not to ue getExtras so you should have two getExtra
Like this :
Intent intent = getIntent();
String ssurname = intent.getExtra("surname");
String slastname = intent.getExtra("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);
I'm still learning AS & java and recently bought an app code but it has "getArguments()" inside of fragment which I'd like to convert it into activity. Help is appreciated.
This is the code :
String weburl = getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
String data = getArguments().containsKey(LOAD_DATA) ? getArguments().getString(LOAD_DATA) : null;
if (data != null) {
browser.loadDataWithBaseURL(weburl, data, "text/html", "UTF-8", "");
} else {
browser.loadUrl(weburl);
}
How do I write the same code in an activity?
I think what you are looking for is how to send info to an Activity, for that you need Intents
Please verify this info: https://developer.android.com/reference/android/content/Intent
Basically:
public static final String EXTRA_MESSAGE = "extra message";
...
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello this is an intent";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
To retrieve that data you have to do:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
I invite you to check this out: https://developer.android.com/training/basics/firstapp/starting-activity#java
Intent extras are the activity equivalent of fragment arguments.
I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.
I'm trying to use startActivityForResult() to get a String from another activity, but I keep getting a NullPointerException whenever I try to retrieve the String from the Intent. Here's what I've got:
//To set up the Intent:
String in = mEditText.getText().toString(); //medittext is EditText that I want String from
Intent i = new Intent(this, ActivityLoaderActivity.class); //activity that started this one
i.putExtra("message", in);
setResult(1);
this.finish(); //cause I'm using startActivityForResult()
//To get the String
#Override
protected void onActivityResult( ... , Intent data) {
String s = data.getStringExtra("message"); //error here
}
I know the error is at getStringExtra() through debugging, but I still can't figure out why it's crashing. Anyone have any ideas?
Maybe you could try using setResult(1, i) and checking in onActivityResult() for the resultCode before getting the extra like this
if(resultCode == 1)
{
//get String extra
}
Hope it helps
Having a very annoying problem with passing data between activities.
This is the code I use to successfully pass the value of a progress bar to a different activity:
public void WhenClicked(View view)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
Okay, so that worked. Now, when I change it to this, it blows up:
public void WhenClicked(View view, String category)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
intent.putExtra("Category", category);
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
I don't understand what the problem is. I've even tried sticking it all into a bundle and adding the bundle as an extra - that just made it crash as well. Maybe I'm being stupid and I've just been staring at my code too long, but any help would be great!
This is my first time with Android and it's killing me!
Thanks in advance guys!
first you need to create bundle object(Bundle bnd=new Bundle();) and next bnd.putString("param1", "test");
next create intent:
Intent myIntent = new Intent(current classname.this,nextactivity.class);
myIntent.putExtras(bnd);
startActivityForResult(myIntent, 0);
In 2nd activity u need to get bundel value like :
Bundle bundle = this.getIntent().getExtras();
String _getData=bundle.getString("param1");
Assuming that you have an activity called QUESTION, you might try to put .class on the end as well as "this" for first param:
Intent intent = new Intent(this, QUESTION.class);
if you don't have a QUESTION activity then that is another problem. I'm assuming your activities are in the same app?
I believe, the activity which should handle this intent action com.android.Test.QUESTION does not understand your category i.e intent.putExtra("Category", category);.
You can try fixing it in 2 ways:
If the receiving activity is from your own application, then try using explicit intent i.e Intent intent = new Intent("youCurrentClass","theClassYouWantToCall"); without feeding additional category, this will launch the specified activity. In case of explicit Intent Android system does not do a comparison with the intent-filters to match Intent Object.
Change the category section in relation to intent receiving Activity.
Hope this helps,
sku