-1

我正在研究 SDK 中的 ApiDemos 示例并尝试将 Menu 示例(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 显示此错误“微调器无法解析或不是字段”,我无法继续。我没有看到任何包含“微调器”的 XML 文件。

4

2 回答 2

0

As per Tim's comment above:

put this line in your onCreate()

mSpinner = (Spinner) findViewById(R.id.spinner);
于 2012-07-11T21:43:34.977 回答
0

您应该将您的setContentView(R.layout.main);右下方移到super.onCreate(savedInstanceState);It's necessary to set the content of your Activity before using its Views 并且您不需要以编程方式实例化微调器视图。只需使用在main.xml文件中定义的 Spinner(我假设您在此文件中有一个 Spinner)。

    /** 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);
    }
于 2012-07-11T22:11:15.633 回答