TextView Not Accessible - java

I am new to Android.I am making a random generator program which on button click generates a number and display it on textview. I am unable to access textview when i type R.id. i don't get the name of the textview but i get the name of the other components. My layout code is:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<TextView android:id="#+id/txt02" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Java code is:-
public void sendMessage(View view) {
// Do something in response to button
EditText editmessage = (EditText)findViewById(R.id.edit_message);
String message = editmessage.getText().toString();
Random r=new Random();
int i1=(r.nextInt(80) +65);
message += "\n " + i1;
// Create the text view
TextView textView = (TextView) findViewById(R.id.); // not able to access R.id.txt02
}

The problem most likely is that your R.java file hasn't been updated with the EditText you have added. In eclipse, you can force an update to do this. Try Project->Clean as mentioned by Michal Z. in the comments.
You can also set the option to build automatically in eclipse by checking the Project->Build Automatically option. This will ensure that eclipse will automatically build your project and update your R.java file every time you save your project.

If cleaning your project doesn't help, I'd check your source files to make sure there isn't a line saying "import android.R" in your imports because that will import the standard Android R object (which doesn't include any of your declared resources). Eclipse does this sometimes when you cut and paste code and it can be frustrating, but deleting the import and cleaning / building your project should fix it.

Related

Cannot find added elements in Tabbed Activity

I created a Tabbed Activity template project using Android Studio.
Then I created a LinearLayout with a nested TextView on it:
<LinearLayout
android:id="#+id/updateView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="707dp"
android:orientation="vertical">
<TextView
android:id="#+id/updateTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
This view is shown on preview, but there's none of it in the built app.
When I try to get a reference to it from my code:
LinearLayout lastUpdate = findViewById(R.id.updateView)
I get null in runtime.
So, how can I modify the code?
Preview screenshot
I solved my problem by moving xml code of LinearLayout upper, than code of AppBarLayout, and modification of dynamic pixels (dp) of ViewPager and my textView

Android Studio cannot find symbol variable

I installed Android Studio about two days ago, I also installed the most updated version of Java and JDK 1.8 (I think it was Java SE 8u101). I am able to edit code and I'm following a tutorial from the official Android Studio website. (Here: https://developer.android.com/training/basics/firstapp/building-ui.html)
However, upon hitting run I've gotten the two following errors:
Error:(24, 57) error: cannot find symbol variable activity_display_message
Code for this:
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
Error:(33, 25) error: cannot find symbol variable EXTRA_MESSAGE
Code for this:
intent.putExtra(EXTRA_MESSAGE, message);
What might I be doing wrong? The emulator pops up and I can get to other apps on the phone but I just can't get my app to load. Should I have installed Java SE 8u102 instead?
I also got a second error message:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
Any help is appreciated.
Below is my code for the first xml file, "activity_main.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Below is my code for "activity_display_message.xml"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.appno1.DisplayMessageActivity">
</RelativeLayout>
Add this to your MainActivity:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
You might have missed this line, which can be found in the "Build an Intent" part.
Refer this for more, from the original page.
I think you might lose the last note in the tutorial.
so you just add android:id attribute to the activity_display_message.xml file and it will be ok!oh actually you should add into the layout element which is the valid location.
My advice is, try to clean the project and build it again. If you want to access newly added resources you need to build the project. Cleaning will help to remove all previously autogenerated resources from project and building will help to create them again + new ones.
You should add EXTRA_MESSAGE as static final, like:
private static final String EXTRA_MESSAGE = "message";
Regarding this error,
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
check that the file res > layout > activity_display_message.xml as the id specified as android:id="#+id/activity_display_message"

Android- Eclipse can't find ImageButton class

I am fairly experienced with Java/Eclipse, but I'm entirely new to Android development and it's proven to be quite an odd beast so far. I'm currently trying to create an application with two image buttons. My XML is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/now_playing" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/StartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/start_button"
android:contentDescription="#string/start_button" />
<ImageButton
android:id="#+id/StopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#drawable/stop_button"
android:contentDescription="#string/stop_button" />
</LinearLayout>
</LinearLayout>
This appears to be correct, but the ImageButtons do not render properly in the Graphical Layout tab and there is a message that says, "The following classes could not be found:
- ImageButton (Change to android.widget.ImageButton, Fix Build Path, Edit XML)".
After looking around online a bit, I found that this question has popped up a few times on StackOverflow, but I was unable to find any satisfactory answers. The most common answer was to clean the project, but this has not done anything for me. Any suggestions?
Sometimes the Eclipse Graphical layout tab is unable to draw stuffs whereas the code is fine. Try to run a simple Activity with setContentView(R.layout.yourxml); and see what happen :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
}
}
Post your LogCat if you still have errors.
EDIT : Oh and I didn't see :
On your second ImageButton, replace
android:text="#drawable/stop_button"
by
android:src="#drawable/stop_button"
Try to import the ImageButton class on java.
import android.widget.ImageButton;

findViewById Error cant find id in XML

I cant find out why my findViewById is not linking to the XML file and it is driving me nuts. The is is in the XML file but it is still not Finding it in the java MainActivity file.
here is my XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editCentimeters"
android:layout_width="wrap_content"
android:layout_height="27sp"
android:layout_alignLeft="#+id/editInches"
android:layout_alignTop="#+id/textCentimeters"
android:ems="5" >
<Button
android:id="#+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editInches"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="#string/convert" />
And here is my Java
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
I was facing the same issue. Just comment out the line which is throwing the error & BUILD the project once. Intellisense (ctrl + space) then will show you the IDs.
You need to take care of 2 things here:-
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
Firstly, the above code should be present in the onCreate() method.
And next, you need to place that code snippet below this line in the onCreate() method, not before it.(I'm assuming that the setContentView is already present, if not, add that)
setContentView(R.layout.main);
use setContentView(R.layout.main); at the onCreate of your Activity
You can refresh project.
And also check ur setContentView(R.layout.YOURLAYOUT_NAME) in Your activity.
Make sure that u are using ur code in activity.
Just clean & build in order to find the ID.
Clean & build didn't work for me. I had to restart Android Studio.

problems with R - ANDROID

when i create a layout called " main.xml ":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button" />
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
when i go to the main to create a button
" button = (Button) findViewById(R.id.button); "
i have an error , i cant find the button in R.id
and same for the spinner
the id dont exist in R.id
help me plz
You're probably importing the wrong version of R. There are two versions which exist: the static R class which exists in android.R and then there is also yourPackageName.R. If you import the former none of your Ids will be found.
Make sure you don't have any files, pictures, etc in your drawable or assets folder with capital letters, spaces, or dashes. Just use plane old lowercase. Also, try to clean your project. Go to project > clean. Then restart eclipse. Hope this helps. Also eclipse won't reconize any changes in XML files without saving first. Make sure to save after you add a button, ect.
Try to verify that there is no errors in your XML files,than try to clean the project.

Categories