So i have this java file which basically creates a list of menu items that will take me to that specific activity. I created the list in java but did not create an .xml file when creating the list. Normally, when creating a new activity, android creates an .xml and java file for both. I only created a java file for it. Can android auto generate an .xml file for my menu class or do i have to recreate it?
Code:
Menu.java
package com.AthleteProgram.x.athleteprogram;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes [] = {"AddAthlete", "Email"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String listPosition = classes[position];
try {
Class myClass = Class.forName("com.AthleteProgram.x.athleteprogram." + listPosition);
Intent intent = new Intent(Menu.this, myClass);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
I tried to create a menu.xml but to no avail:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.AthleteProgram.x.athleteprogram.Menu">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
What do I do to get a visual .xml file so I can see exactly what I am working with? Thank you!
You are missing the part that adds the xml to your class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu); //This line add the view to your activity
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes);
setListAdapter(adapter);
}
You can also create your layout separately by doing right click on res -> New -> Android resource file . Give a name to it and set its Resource type value to "layout".
Then in your Menu.java file add this line setContentView(R.layout.menu);
within onCreate
method after super.onCreate(savedInstanceState);
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.