我正在研究SDK中的ApiDemos示例 并尝试将菜单示例(App / Menu / Inflate与Menu-MenuInflateFromXml.java分开)。
package my.android.Menu;
import my.android.Menu.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MyMenu extends Activity {
/**
* Different example menu resources.
*/
private static final int sMenuExampleResources[] = {
R.menu.title_only, R.menu.title_icon, R.menu.submenu, R.menu.groups,
R.menu.checkable, R.menu.shortcuts, R.menu.order, R.menu.category_order,
R.menu.visible, R.menu.disabled
};
/**
* Names corresponding to the different example menu resources.
*/
private static final String sMenuExampleNames[] = {
"Title only", "Title and Icon", "Submenu", "Groups",
"Checkable", "Shortcuts", "Order", "Category and Order",
"Visible", "Disabled"
};
/**
* Lets the user choose a menu resource.
*/
private Spinner mSpinner;
/**
* Shown as instructions.
*/
private TextView mInstructionsText;
/**
* Safe to hold on to this.
*/
private Menu mMenu;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
mSpinner.setId(R.id.spinner);//Error Line
mSpinner.setAdapter(adapter);
setContentView(R.layout.main);
}
}
IDE显示此错误“微调器无法解析或不是字段”,我不能去头。 我没有看到任何包含“spinner”的XML文件。
答案 0 :(得分:0)
根据蒂姆上面的评论:
将此行放在onCreate()
mSpinner = (Spinner) findViewById(R.id.spinner);
答案 1 :(得分:0)
您应该将setContentView(R.layout.main);
移到super.onCreate(savedInstanceState);
的正下方。在使用其视图之前必须设置活动的内容,而不需要以编程方式实例化微调器View。只需使用 main.xml 文件中定义的微调器(我假设您在此文件中有一个微调器)。
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
// mSpinner.setId(R.id.spinner);//Error Line
mSpinner = (Spinner) findViewById(R.id.you_spinner_id); //you_spinner_id must be defined at main.xml
mSpinner.setAdapter(adapter);
}