TextView not Found - android

This is driving me absolutely crazy and Im not sure what is going on.
I have an xml layout with a TextView within a clickable RelativeLayout.
<RelativeLayout
android:id="#+id/bg_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/almost_black"
android:clickable="true"
android:onClick="goToBG"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Go To B.G."
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/bg_arrow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-10dp"
android:src="#drawable/arrow_icon" />
<TextView
android:id="#+id/current_bg_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/bg_arrow"
android:text="3"
android:textColor="#color/holo_blue"
android:textSize="22sp" />
</RelativeLayout>
Now in my code I try to update the textview "current_bg_count"
private void updateBGCount(){
try{
RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
TextView bgCountTV = (TextView) bgSection.getChildAt(2);
bgCountTV.setText(tempBG.size());
}
catch(Exception e){
e.printStackTrace();
Logger.d(TAG, "exception in updateBGCount");
}
}
This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:
TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());
it gives the same error.
All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?

You need to convert your size of the ArrayList to a String at this line
setText(tempBG.size())
Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.
So change it to
setText(String.valueOf(tempBG.size()));

Try setting your tempBG.size() to string like this
bgCountTV.setText(""+tempBG.size());
This should work

Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.

the Problem is with this code
bgCountTV.setText(tempBG.size());
tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id
What you can do
bgCountTV.setText(tempBG.size()+"");
OR
bgCountTV.setText(String.ValueOf(tempBG.size()));
OR
bgCountTV.setText(tempBG.size().toString());

Related

Getting all checkboxes, radio buttons, ratings from an expandable list view: android

I have an expandable list view that houses a short survey (3-4 fields for a user to insert data about) per record that we have. Then we have a button that they can hit to submit the data to our server. When they hit the button I need it to grab all the data in the surveys and send it to our site. I have the sending portion of it figured out; what I'm struggling with is grabbing all the data from the survey.
I've looked through many of the related posts/google results and attempted to implement and adapt their solution for listviews to expandedlistviews but I have not been able to get any of them working. I've spent quite a few hours trying to get some workable solution but alas, I cannot figure it out.
I think part of my problem is I'm not exactly certain how children views come into play for expandable list views. In any event below is my code in hopes that someone smarter than me can help me solve this problem.
So I have my ExpandableListView fragment_survey.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="edu.ucr.aum.fragments.SurveyFragment$PlaceholderFragment">
<ExpandableListView
android:id="#+id/list_view_survey_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/rowPaddingMedium"
android:layout_marginRight="#dimen/rowPaddingMedium"
android:divider="#null"
android:dividerHeight="0dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay" />
<TextView
android:id="#+id/tvResponseCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list_view_survey_list"
android:text="Response Code: "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit Survey"
android:layout_below="#+id/tvResponseCode"
android:id="#+id/btnSubmitSurvey" />
</RelativeLayout>
And here is my row xml list_view_survey_row.xml:
<TextView
android:id="#+id/tvSurveyTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/surveyTitle" />
<TextView
android:id="#+id/tvSurveyShared"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/surveyShared"
android:layout_below="#+id/tvSurveyTitle"
/>
<RadioGroup
android:id="#+id/radioShared"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSurveyShared">
<RadioButton
android:id="#+id/radioButtonYes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/yes"
android:focusable="true"/>
<RadioButton
android:id="#+id/radioButtonNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/no"
android:focusable="true"/>
</RadioGroup>
<TextView
android:id="#+id/tvSurveyReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/surveyReason"
android:layout_below="#+id/radioShared" />
<TextView
android:id="#+id/tvSurveyRating"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/surveyRating"
android:layout_below="#+id/tvSurveyReason" />
<CheckBox
android:id="#+id/checkBoxInterest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/surveyInterestPositive"
android:layout_below="#+id/tvSurveyRating"
android:focusable="true" />
<CheckBox
android:id="#+id/checkBoxPrivacy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/surveyShareLevelPositive"
android:layout_below="#+id/checkBoxInterest"
android:focusable="true" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrivacy"
android:focusable="true" />
<View android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF00FF00"
android:layout_below="#+id/ratingBar" />
Then I have a fragment (fragment_survey.java) which adds an onclicklistener to the button (btnSubmitSurvey) in the fragment_survey.xml file. That onClickListener() function calls two methods (one to get the data and one to send that data).
this.buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getData(v);submitSurvey();
}
});
public void getData(View view) {
//get data here
}
public void submitSurvey() {
// Create a new HttpClient and Post Header
String url;
if(Singletons.Debug.debug) {
url = "url for postback";
} else {
url = "url for postback";
}
NetworkAsyncTask nat = new NetworkAsyncTask();
try {
String response = nat.execute(url).get();
//Log.d(TAG, "Response: " + response);
} catch(ExecutionException e) {
} catch(InterruptedException e) {
}
}
You should create a data structure like an array to store the survey data, I would index it by group and/or child position, whatever makes sense for the data. Then you should add listeners for each question input, when a question is answered yes, add into that data structure the answer at the appropriate location. Then when you press the submit button it can iterate over that data structure, prompt for unanswered questions or fill out default values, gather it all together and then submit.
Basically there's no easy/built in way to access the individual rows of your dynamic/reusable rows that I have been able to find. When the context of which row is important I've found the best way is to have my own data structure that uses the group/child position to find which button is being pressed since every row has a button with the same id. I also store the group position/index in the button using setHint on creation so that in the context of the listener i can determine the position.
I had to do this via a horrific hack-job, but it works. I added a button for each row that will allow the user to "save" the row. When they save the row then the data is saved to my data structure. Unfortunately to get this working properly I had to create a custom OnClickListener which takes the view as an argument and from there I can scrape the elements from the view.
It's not an elegant or good solution; but it works.

button press in Android app creates casting error

I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)

Android;Body of ProgressDialog?

I saw this source as layout of AlertDialog in res\layout folder of Android SDK:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/body"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:paddingLeft="8dip"
android:paddingTop="10dip"
android:paddingRight="8dip"
android:paddingBottom="10dip">
<ProgressBar android:id="#android:id/progress"
style="#android:style/Widget.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="10000"
android:layout_marginRight="12dip" />
<TextView android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</FrameLayout>
I tried to find TextView that it's id is "message" like this:
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
It works fine.Then I tried to find LinearLayout that it's id is "body" like this:
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
But eclipse show this error:
body cannot be resolved or is not a field
This is snippet code:
ProgressDialog dialog = new ProgressDialog(WriteOpinionActivity.this);
dialog.setMessage("some text");
dialog.show();
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
Why this error occurs and how I can reach "body"?
Basically, not all resources are accessible to You from platforms res folder.
E.g. I'm able to find #+id/message in global_actions_item.xml, usb_storage_activity.xml, alert_dialog_holo.xml, js_prompt.xml, progress_dialog_holo.xml, progress_dialog.xml, alert_dialog.xml (for API 17), and so, there's no warranty that You get id which defined in progress_dialog.xml (it can be defined in another file and just because of the same name You is able to find it in progress dialog).
Actually, public resources are defined here, however, it looks quite outdated, so some resources might be missed. Checkout these questions about lists of all accessible resources: 1 and 2
Use this instead.
TextView tvMessage = (TextView)dialog.findViewById(R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(R.id.body);
You weren't actually using android.R.id.message in the first place. #+id/message means you are creating a new id resource named message.
To use an id that exists in the Android framework, you can see here that you use #android:id/progress. And in source, you use it like you had it: android.R.id.progress.
Otherwise, use android:id="#=id/myIdentifier" in XML and
findViewById(R.id.myIdentifier) in code.
http://developer.android.com/guide/topics/resources/overview.html

Android ellipsize does not work properly

I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))

Displaying an integer variable in TextView

I have an int that depends on users input. How do I display that int on the screen?
Here is what I have been attempting:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
Also here are my defined variables, you may have seen my post not too long ago about this program:
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra("MESSAGE_HW", 0);
int OTW = intent.getIntExtra("MESSAGE_OTW", 0);
int HPD = intent.getIntExtra("MESSAGE_HPD", 0);
I am trying to display the int HW on the screen, any help is greatly appreciated! Thank you!
android:text="#int/HW" // What goes here, This is where i am confused
A default value can go there. Something that would tell you that the TextView has not been populated yet.
How do i display that int on the screen?
To display the int on the screen inside the TextView you have shown us. You need to get the TextView in your Activity with something similar to the following:
TextView textView = (TextView) this.findViewById(R.id.textViewId);
Then you can set the text on that textView with the following:
textView.setText(String.valueOf(HW));
Here is an example of adding the id to your xml:
<TextView
android:id="#+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
When you get the users input, do this:
TextView hwTextView = (TextView)findViewById("R.id.hwTextView")
hwTextView.setText(String.valueOf(yourIntVariable));
Note this means you'll have to add an id attribute to your TextView with the value of "hwTextView"
Building on prolink007's answer, you could also omit that line then call setText, java side.
<TextView
android:id="#+id/hw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".DisplayMessageActivity" />
TextView HWgetText = (TextView)findViewById("R.id.hw");
HWgetText.setText(String.valueOf(HW));

Categories

Resources