Intent i = getIntent();
categorie_label = i.getStringExtra("categorie_label");
TextView txtrank = (TextView) findViewById(R.id.catid);
txtrank.setText(categorie_label);
Why the code below returning a NullPointerException ? Please help
Your activity probably did not find the TextView with the specified ID in it´s layout and thus it is null. You should check if it is contained in that layout.
Related
I'm trying to get a text from a TextView so I tried to use getText() method but didn't work. I'm using fragments only, so there's a way I can get my text from the TextView?
Ps. I tried with getEditText() but I get NULL.
you are probably calling TextView textView = (TextView) findViewById(R.id.yourInFragmentTextView); on your activity layout?
You need to get the reference to the actual view in the fragment.
First you need a reference to the inflated fragment and than call
TextView textView = (TextView) myFragment.findViewById(R.id.yourInFragmentTextView);
String text = textView.getText().toString();
String str = textview.getText().toString(); should work
try clearing cache and restart
I have a list view with a custom adapter that I am attaching and It needs to have a header with some modifiable information in it. I have created an xml layout for the header, and when its applied everything displayes correctly. But when I try to reference the elements in the header view and set there contents, I receive a Resource not found exception. The exception is specifically thrown at "name.setText(task.getName());". I presume it would be thrown at each of the 3 elements I reference If it go that far.
What is the correct way to do this? Are the resources im referencing not in scope? How can I modify them correctly?
The onCreate of my activity containing the list looks like what is below:
super.onCreate(savedInstanceState);
setContentView(R.layout.taskview);
Object sTask = getIntent().getSerializableExtra("task");
TaskNode task = (TaskNode)sTask;
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView name = (TextView) header.findViewById(R.id.hname);
TextView description = (TextView) header.findViewById(R.id.hdescription);
TextView completion = (TextView) header.findViewById(R.id.hcompletion);
name.setText(task.getName());
description.setText(task.getDescription());
completion.setText(task.completion());
taskList = (ListView) findViewById(R.id.taskList);
taskList.addHeaderView(header);
TaskViewListItem adapter = new TaskViewListItem(this, getApplicationContext(), task);
taskList.setAdapter(adapter);
From the exception you get one from getName(), getDescription() and completation() is returning a numeric value. When you call setText with an int value android will lookup inside R in order to find the String with the id you provide as parameter to setText. If a String with that id does not exist the android.content.res.Resources$NotFoundException exception will be thrown. You can get the String representation of a number with:
String value = String.valueOf(numericValue);
I was wondering how to give a TextField an 'id' in java. So I am pretty much wanting to know how to give the TextField android:id="#id/NAMEHERE" but do it in java. Here is the code so far:
TextView taskname = new TextView(this);
taskname.setText("Task " + tasknumber);
tasklayout.addView(taskname);
What do I need to add to give it an id?
If your question is to how to set id into activity, then understand purpose of id.
Id is specially required when you want to fetch view reference into activity class, where view has been declared into xml file. But in Activity, if you are creating any view, by
TextView taskname = new TextView(this);
here, you already have view reference, but if you still want to set id, then you can use method
taskname.setId(10002);
Try this
static final int TEXT_ID = 80001;
TextView taskname = new TextView(this);
taskname.setId(TEXT_ID);
and later in your code you can refer to this textview by
TextView t = (TextView)findViewById(TEXT_ID);
or
TextView t = taskname;
I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);
If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.
In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}
You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)
Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());
i have two activities each contain its own textview any one help me that when i click the one textview text the text goes to the another textview of the second activity please any on help me in this problem
In activity A.
TextView aTv = findViewById(R.id.aTextView);
String aText = aTv.getText().toString();
Intent i = new Intent(bClass.class); // your other activity
i.putExtra("myText", aText);
startActivity(i);
In the onCreate of Activity B:
String aText = this.getIntent().getStringExtra("myText");
TextView bTv = findViewById(R.id.bTextView);
bTv.setText(aText);
Is that what you were looking to do? I'm into a few beers so there may be a few typo's but that's the idea. :)