I'm trying to decode HTML entities, currently my code is:
val str = name
val textView = findViewById<View>(R.id.text) as TextView
textView.text = Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT)
and here the error message is:
kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextView
Your error has nothing to do with the HTML handling, you're getting the exception on this line, because findViewById is returning null, and then the cast to TextView is failing:
val textView = findViewById<View>(R.id.text) as TextView
Why this happens exactly is hard to tell without context, but the issue is that the View with the ID text was not found.
If you're in an Activity, make sure you're doing this after you call setContentView.
If you're in a Fragment, make sure you're doing this after the onCreateView method has run.
In either case, make sure you're actually using a layout that contains a TextView with the ID #+id/text.
Additionally, you're looking up the TextView as a View first and then casting it, you could do either of these instead:
val textView = findViewById<TextView>(R.id.text)
val textView: TextView = findViewById(R.id.text)
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 10 textviews each with the following id(s) tv1, tv2, tv3, etc. and each have text in them. These textviews are clickable, I saved the resource ID of the clicked textview in sharedprefrence. This ID is an int.
Now using this resource ID, can I somehow get text attached to this resource(TextView here). Below is code the code I am trying to use, but it gives a null pointer exception.
if (!sharedPref.getString(String.valueOf(i), "error").equals("error")) {
int u = Integer.parseInt (sharedPref.getString(String.valueOf(i), "error"));
System.out.println("For "+i+" object Value of id retrieved is-"+u);
String dohass= ((TextView) findViewById(u)).getText().toString();
}
I have checked value of u and is correct(correct resource id), but the above code says I am using getText on a null object.
Here are the logs
java.lang.RuntimeException: Unable to start activity ComponentInfo{dhritiapps.tulsiramayan/dhritiapps.tulsiramayan.Fav}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
First you have to get reference of your textview.
TextView textview= ((TextView) findViewById(R.id.youtextviewid));
then you can get the string value by calling getText on TextView reference.
String yourtext= textview.getText().toString();
Make sure you perform this step after setContentLayout() method call in onCreate. If tried before that, it will give null pointer exception because it will not be able to find your textview.
Get reference of texview.
TextView textview= (TextView) findViewById(R.id.textviewid);
call this method
String yourtext= textview.getText().toString();
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.
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;