androidx.appcompat.widget.AppCompatEditText error - java

Full Error:-
> androidx.appcompat.widget.AppCompatEditText{39b5cb6VFED..CL. .F....... 188,641-977,812 #7f080097 app:id/edittext aid=1073741824}
This image will show you that error...
https://i.stack.imgur.com/ZpVnh.png
Detail:-
i got this error when i was passing data from one activity to another in android.
Activity 1st Code:-
EditText e1;
e1 = (EditText) findViewById(R.id.edittxt);
public void jumpbutton(View view) {
Intent i1 = new Intent(this,ThirdActivity.class);
i1.putExtra("user",e1.toString());
startActivity(i1);
}
Activity 2nd Code:-
TextView txt1;
txt1 = (TextView) findViewById(R.id.txtvw);
Bundle bd1 = getIntent().getExtras();
String st1 = bd1.getString("user");
txt1.setText(st1);

i successfully solve this error, by adding this
.getText()
into the first activity code.
Like this:-
public void jumpto3(View view) {
Intent i1 = new Intent(this,ThirdActivity.class);
i1.putExtra("user",e1.getText().toString());
startActivity(i1);
}
Then my error is solved.
After code is edit:-
you can see the result in this image

On the first Activity
public void openActivity2(){
EditText editEmail =findViewById(R.id.textEmail) ;
Bundle bundle = new Bundle();
Intent i = new Intent(this, MainActivity2.class);
bundle.putString("emailvalue", editEmail.getText().toString());
i.putExtras(bundle);
startActivity(i);
}
Then in your second Activity u can get the value of EditText like that
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = getIntent();
String text = i.getStringExtra("emailvalue" );
System.out.println(text);
}
});

you are missing .getText().toString() somewhere. find that out and fix it.

Related

Displaying "Happy birthday null" in the second activity of my app

Main Activity
`public class MainActivity extends AppCompatActivity {
private Object Context;
public static final String MSG="com.mp.exampleapp.ORDER";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
`Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthdaygreeting);
Intent intent = getIntent();
String msg = "HAPPY BIRTHDAY "+intent.getStringExtra(MainActivity.MSG);
// receive the value by getStringExtra() method
// and key must be same which is send by first activity
TextView textView= (TextView)findViewById(R.id.textView2);
textView.setText(msg);
}
After running this program - after clicking the button- the message that's getting displayed is "HAPPY BIRTHDAY null". I don't know what's going wrong.
What is wrong?
The place where you are passing the put extra, is in getIntent(). This means that you are giving an intent extra to the previous activity.In this case, when you go back, and then get that extra, you will get it.But not on the next activity. Now you get null because that extra does not exist for that activity. So, it comes null.
How to solve?
Instead of using this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
use this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
startActivity(new Intent(this,birthdaygreeting.class).putExtra(MSG,str)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
How do I know it will help me?
It will help you because here in the new intent which you are giving, there the extra is being given. So, that value is being sent.
Create an intent then add your data in it like the code below.
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,birthdaygreeting.class);
intent.putExtra(MSG,str);
startActivity(intent) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}

sending text to new activity

i have a relative view with different images each time i click on an image i want a unique text to be sent to another activity. i have been able to achieve this for the first image but i cant get the second image to send the text to next activity.
my code for the first-activity:
b1 = (ImageView) findViewById(R.id.native_amala);
b2 = (ImageView) findViewById(R.id.native_fufu);
b3 = (ImageView) findViewById(R.id.native_jollof);
b4 = (ImageView) findViewById(R.id.native_ofada);
b5 = (ImageView) findViewById(R.id.native_porridge);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("Amala",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("fufu",foodName1.getText().toString());
startActivity(intent2);
}
});
}
My second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_cart);
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("Amala");
foodView.setText(str);
intent.putExtra(String key, String value) is the format.
Have a unique key for all putExtra functions and use that key in getStringExtra
You should use Same KEY .
Intent putExtra (String name,
Parcelable[] value)
intent2.putExtra("value",foodName1.getText().toString());
And
Bundle bundle=getIntent().getExtras();
String str=bundle.getString("value");
foodView.setText(str);
You basically need to understand how putExtra works.
You can put some value with keys, for e.g.
intent.putExtra("Amala",foodName1.getText().toString());
Here, Amala is the key and foodName1.getText().toString() is the value.
Then in your next activity you can retrieve the value from key.
i.e. you can do,
String str = intent.getStringExtra("Amala");
In your click listener of b2 ImageView, you are mentioning the key fufu but in your second activity you are getting Amala.
You need to keep common key for all click listeners and then retrieve data from that key in second activity.
You need to keep common key for all click listeners like this way to send data
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("data",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("data",foodName1.getText().toString());
startActivity(intent2);
}
});
now receiv e data in other activty like this
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("data");
foodView.setText(str);
You using the key is fufu.
intent2.putExtra("fufu",foodName1.getText().toString());
So u get the value then used that key.so change getStringExtra key name change "amala" to "fufu"
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("fufu");
foodView.setText(str);
For Sending Some Text form one Activity to another activity
You can Simply use putExtra() function
putExtra(Sting Key, String value);
Let say you have String variable
String str = "Hello";
After when go to next Next Activity
Intent Intent = new Intent(getApllicationContext(),Your_Activity.class);
intent.putExtra("Hello");
startActivity(intent);
When you want this variable in Your_Activity then simply in onCreate() method
Intent intent = getIntent();
String str = intent.getStringExtra("Hello");
Use this
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("VALUES", someValues);
startActivity(intent);
Access that intent on next activity
String s = getIntent().getStringExtra("VALUES");

sending text from editText to ListView (2 activities)

i am new to Andriod and programming. i am trying to make a to do list app, which will contain a listView, Button(add) in the main layout. when i click add i want it to go to another activity which will contain a editText and a add button. when i click the add button i want to update the list in my main activity. Now i was able to get the information from the second activity but when i try to add it in my list it over writes it.
How can i update my list as soon as my main activity appears again.
Here's what i have so far :
MainActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.bAdd);
list = (ListView) findViewById(R.id.lvList);
addItems(); // i think this is the error but i dont know how to fix it.
alList = new ArrayList<String>();
aaList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alList);
list.setAdapter(aaList);
}
public void clickAdd(View v) { //when clicking the add button(add)
Intent intent = new Intent(MainActivity.this, AddItem.class);
startActivity(intent);
}
private void addItems(){
String s = getIntent().getStringExtra("item");
aaList.add(s);
aaList.notifyDataSetChanged();
}
AddItem class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
et = (EditText) findViewById(R.id.etAdd);
bt = (Button) findViewById(R.id.badd);
}
public void add(View v){ //when clicking the add button(bt)
edit = et.getText().toString();
Intent intent = new Intent(AddItem.this, MainActivity.class);
intent.putExtra("item", edit);
startActivity(intent);
}
Can you please tell me where and why i am going wrong? Thank You
First of all, you should use startACtivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Then in your second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
And back in your fist one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
and then, your addItem should do the trick:
private void addItems(String s){
aaList.add(s);
aaList.notifyDataSetChanged();
}

taking variable from another class

I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.

Categories