This code is within a method in a class (not main):
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
The textview seemes not to be recognized in this part: R.id.moodouttw
The textview moodouttw is located within a layout xml.
Can someone please helpout with code to correctly format the code above?
(I've tried to minimize code at first if it could be solved with any further info)
LOGCAT: 940Kb cant post
is not iR.d is just R.id
the proper way will be
findViewById(R.id.moodouttw);
this is the complete correct sentence
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
ok as you stated at your xml file, the problem is that your TextView id is moodtw and you are doing R.id.moodouttw;
so you need to do this
TextView moodouttw = (TextView) findViewById(R.id.moodtw);
Related
I'm trying to build a timetable app and there I will need a lot of dynamical Textviews, the only way I know is to declare every Textview for it self like
MoFa1 = (TextView) findViewById(R.id.MoFa1);
MoFa2 = (TextView) findViewById(R.id.MoFa2);
MoFa3 = (TextView) findViewById(R.id.MoFa3);
MoFa4 = (TextView) findViewById(R.id.MoFa4);
MoFa5 = (TextView) findViewById(R.id.MoFa5);
MoFa6 = (TextView) findViewById(R.id.MoFa6);
MoFa7 = (TextView) findViewById(R.id.MoFa7);
MoFa8 = (TextView) findViewById(R.id.MoFa8);
MoFa9 = (TextView) findViewById(R.id.MoFa9);
MoFa10 = (TextView) findViewById(R.id.MoFa10);
Now I want to know if there is another easier way to declare multiple views.Thanks in advance
How are your views displayed? Since you're creating a timetable I think your views are displayed in a grid. You can use GridLayout or you can use a RecyclerView. Here its an excellent article about using RecyclerView.
EDIT: Here its another article using RecyclerView with GridLayoutManager
I don't know how they are displayed, but you could use a gridlayout and add them dynamically by looping throught cells.
http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources
http://android-er.blogspot.ca/2014/09/insert-view-to-gridlayout-dynamically.html?m=1
If these links actually help you, I will describe its content here.
Use butter knife http://jakewharton.github.io/butterknife/
#BindView(R.id.MoFa5) TextView mofa5;
That's it. No findviewbyid.
I'm trying to change my TextView text from the code.
This is what my xml looks like:
XML:
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
And the code:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
I'm getting an error on my device and the application stops.
I tried to show a TextView (not connected to an XML TextView) and it worked.
Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)
You need to specify the layout you are using first, before finding views.
Java:
// Specify the layout you are using.
setContentView(R.layout.yourlayout);
// Load and use views afterwards
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
Kotlin:
// Specify the layout you are using.
setContentView(R.layout.yourlayout)
// Load and use views afterwards
val tv1: TextView = findViewById(R.id.textView1)
tv1.text = "Hello"
Click Here to study exactly you want to know
remove this.. setContentView(tv1);
I got the same problem. My app was stopping too.
Actually, I was writing the code outside of the function/method. So to fix this problem, these lines
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
must be inside a function/method. (can be user defined)
(I am new to android studio so I don't know the reason behind the problem but I only know how to fix this. Maybe this helps new ones despite this question being 8 years old.)
I'm new at this and is the first time I try to do an android app. I'm trying to do a rock, paper & scissors I know I haven't finish the code but I'm having some error....
I see a few places that could be the problem, but one that could be causing you trouble is where you have
else (computerplay.equals("5"))
you are missing the if, you need something like
else if(computerplay.equals("5"))
in a few places.
Also, you need to reference the TextView that's in your layout, so instead of
TextView tv = new TextView(this);
you need something like
TextView tv = (TextView) findViewById(R.id.textview);
and dont call setContentView(tv).
hii every buddy ,
am not getting how to load parsed data into an existing textview(existing layout) in android,
insted of loading data to the new textview like how they r doing in the following tutorial(check the following link)
check this link for tutorial
In the tutorial they do:
TextView something = new TextView(context);
As you don't want to do so (good decision), you get the reference to the existent TextView:
TextView something = (TextView)findViewById(R.id.the_id_you_gave_it_in_the_xml);
// then:
something.setText(theParsedString);
Try this code using existing Layout
Textview name=(TextView)findViewById(R.id.textView1);
String txt="";
for(int i=0;i<sitesList.getName().size();i++)
{
txt=txt+sitesList.getName().get(i).toString();
}
name.setText(txt);
Sowmya It seems that you are creating multiple textviews & trying to update those textviews .In that case you need to use setTag() and getTag() .
There is an example you can find here
Regarding your answer:
we r declaring textview array so am
unable to load to existing textview
I have never implemented such a textview array instead the best practice would be to
for (String s : stringarray) {
TextView tv = new TextView (this);
tv.setText(s);
tv.setTag("1");
linearlayout.addView(tv);
}
*EDIT:*For those enthusiast out there I found 2 more methods:
One is using setId() to manually set ID of that textview & then find it by findViewById()
Another method(is though not implemented by me , but is a suggestion by my colleague so dont kill me if it doesn't works) is to store textview objects in an arraylist & then access them & do whatever you want whoa!
Is there anywhere that I can find documentation on the scope of the XML files? I have an app I am currently working on and have been struggling with getting a feature to work and it seems that the problem I am having is that I am trying to access an element in an XML file that must be out of scope. To simplify the layout, my project has main.xml, sub.xml, main.java, and sub.java files in it. As you can probably guess, main.java works with main.xml and sub.java is working with the elements in sub.xml. Here's where the issue comes in, I have a TextView element that is created in main.xml that I would like to modify the text in, but the action that would trigger it will occur in sub.java. I can't figure out how to change it from sub.java, and I can't figure out how to move the element into sub.xml. The code I am using is pretty simple:
TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
I get a FC every time I run the app, but if I move the code into main.java, it runs flawlessly. If anyone can offer any ideas, or point me in the direction of some documentation that would explain what java files can access what elements in which xml files, that would be awesome! Sorry for the novel, but I'm just struggling to get the point across. Thanks.
try like this Bryan in Main.xml file it works with no issue...........Declare first & then Initialize it...
public class Main extends Activity {
static TextView tv;
static Button submit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv = (TextView) findViewById(R.id.header_text1);
}
}
Activity.findViewById(int) only works if that view is in the activity's layout. So no, you can't refer to a view in main.xml because that layout doesn't apply to sub.
Do you have any TextViews in sub.xml called myTitle?
You can access the the textview of main.java(main.xml) in submain.java as follows
In main.java write the following code
static TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
and u can access this submain.java as
Main.titleText.setText(filePath);