Could not find some components by calling findViewById
-
26-10-2019 - |
Question
findViewById can't retrieve a button and a radiusBar even though their id exists. code crashes when initiating saveButton or radiusBar.
acticity:
private Button saveButton;
private EditText titleText;
private SeekBar radiusBar;
private TextView radiusValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
titleText = (EditText) findViewById(R.id.title);
radiusValue = (TextView) findViewById(R.id.radiusValue);
saveButton = (Button) findViewById(R.id.confirm);
radiusBar = (SeekBar) findViewById(R.id.radiusBar);
xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="3dp"
android:text="@string/title"
android:textColor="#00ffff"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dp" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/hello"
android:textColor="#ffffff"
android:textSize="15sp" />
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:apiKey="my google maps api key"
android:clickable="true"
android:enabled="true"
android:padding="5dp" >
</com.google.android.maps.MapView>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:text="@string/proximity"
android:textColor="#00ffff"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/radiusText" >
<TextView
android:id="@+id/radiusValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:text="@string/radius"
android:textColor="#00ffff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<SeekBar
android:id="@+id/radiusBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radiusText"
android:max="1000"
android:padding="5dip"
android:soundEffectsEnabled="true" />
<Button
android:id="@+id/confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radiusBar"
android:padding="3dp"
android:text="@string/confirm"
android:textSize="20sp" />
</RelativeLayout>
i am new at android. even though i used old school comment out way or ddms i couldn't find why this is happening. thanks for the help.
Solution
When you say "crash", you should go on to say "... and this is the log output with the error message". Anyway, the resource IDs certainly exist, as your code compiles. However, a common build system error with Android is that your resource IDs can be wrong after you reorder some XML. I think you'll find that the error is a ClassCastException. The solution: clean your repo (e.g., with ant clean
, or some button/menu in Eclipse) and recompile everything from scratch.
OTHER TIPS
Whenever I had a similar problem, I usually could solve it by using the following menu in Eclipse: Project->Clean
You can see look at the class that Android projects automatically generate
Eclipse is a bit "too" smart in this case. It will find an ID that is part of any layout. Not just the one you assigned as the setContentView() layout.
So you have a valid ID, but it is not valid at runtime, when the layout does not contain the ID.