Reputation: 619
Just added a new button to my already-working-fine layout, but the findViewById function seems to be angry with something I don't get to understand.
Here's a bit of the layout:
<LinearLayout
...
>
<ListView
android:id="@+id/my_lovely_list"
android:layout_width="fill_parent"
android:layout_weight="1"
/>
<Button
android:id="@+id/my_lovely_butt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_b"
android:layout_weight="0"
android:clickable="true"
/>
</LinearLayout>
And here's a bit of the coding:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
list_o = (ListView)findViewById(R.id.my_lovely_list);
butt_o = (Button)findViewById(R.id.my_lovely_butt);
...
}
So, the big mistery is that the ListView is found without any problem, but the Button won't by any means. I've already tried cleaning the Proyect, and look throught the posts I've found here... but still don't get to find the problem! Any thoughts?
Upvotes: 10
Views: 27791
Reputation: 939
Make sure to go through these processes.
1: Make sure you imported your project R file.
import packagename.R
2: Clean your project
3: If the above two solutions doesn't work, do invalidate your cache and restart
Upvotes: 3
Reputation: 1531
In Android Studio Build
-> Clean Project
resolved the problem for me.
Upvotes: 1
Reputation: 9
while, just by clean you project, rebuild it, or import the R file manually, it might a sad thing to find the id of specified view component still cannot find by the activity. and then, if you check the detailed content of the R file, you will find the id has not generate, and then, what you need to check, if there are something wrong with your own /res file, for example, the name of the layout file, drawable file name, or the variant name of the menu file .etc. all of these could just make the R file cannot generate correctly, or even worse, the R file does not generate out at all.
Upvotes: 0
Reputation: 6485
Some times the file R is not regenetated. Try :
No worry that's is just some common eclipse/android bug it happen often (Well some other times it can also be a small syntax error somewhere in your project but i assume that you already checked that x)
Good luck
Upvotes: 6
Reputation: 15847
import yourpackagename.R;
instead of android.R;
import R
of your package
Also Clean
your project that will refresh your entire project then you will also find ID
of button also
Upvotes: 21
Reputation: 2077
If its a syntax related problem, then its got to do with compile time stuff.
If its a problem when the code executes, make sure the layout is inflated. If the listview is found, then the button also must definitely be found.
Upvotes: 4