I have a class that is supposed to call another activity depending on which button has been clicked. This is done by checking the available IDs.
My problem is: The view that is passed to my class has no id, or rather, its value is NO_ID. What puzzles me is that the view, which is a button, does have an ID.
public class StrengthOrganizer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_strength_organizer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_strength_organizer, menu);
return true;
}
public void notifyActivity(View view) {
Intent intent = null;
int id = view.getId();
switch(id){
case R.id.button_log_book_launcher:
intent = new Intent(this, LogBook.class);
break;
case R.id.button_programming_launcher:
intent = new Intent(this, Programming.class);
break;
case R.id.button_visualizer_launcher:
intent = new Intent(this, Visualizer.class);
break;
default:
return;
}
startActivity(intent);
}
}
The corresponding XML file looks like this:
<?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="vertical" >
<Button android:name="@+id/button_log_book_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="@string/button_log_book_launcher"
android:onClick="notifyActivity" />
<Button android:name="@+id/button_programming_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="@string/button_programming_launcher"
android:onClick="notifyActivity" />
<Button android:name="@+id/button_visualizer_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="@string/button_visualizer_launcher"
android:onClick="notifyActivity" />
</LinearLayout>
Why does the view object have no ID if it’s been give in the XML file?
You used
android:nameinstead ofandroid:idin your XML to assign the ids. Use the latter instead.