Android 将微调器充气到视图中时findViewById错误

Android 将微调器充气到视图中时findViewById错误,android,android-xml,android-inflate,Android,Android Xml,Android Inflate,我有一个微调器,选中该微调器后,应向视图中添加一个新微调器。但当我尝试充气时,我在这一行上得到了一个错误: LinearLayout addSpinner =(LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout); findViewByID为红色,在鼠标悬停时显示此错误: Cannot resolve method 'findViewById(int)' 我的整个java类代码如下: pu

我有一个微调器,选中该微调器后,应向视图中添加一个新微调器。但当我尝试充气时,我在这一行上得到了一个错误:

LinearLayout addSpinner =(LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);
findViewByID为红色,在鼠标悬停时显示此错误:

Cannot resolve method 'findViewById(int)'
我的整个java类代码如下:

public class Portfolio extends ActionbarMenu {



    //get beer details from bundle
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_portfolio);

        final Spinner portfolioType = (Spinner) findViewById(R.id.portfolioSpinner);
        portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

                String portfolioChoice = portfolioType.getSelectedItem().toString();

                Toast.makeText(getApplicationContext(), portfolioChoice, Toast.LENGTH_LONG).show();

                Log.d("portfolio" , portfolioChoice);

                if( portfolioChoice.equals("All")){
                    //get userID
                    //get user data
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
                    String userID = prefs.getString("userID", null);

                    //construct url
                    String url = "myURL"


                    //async task goes here
                    new PortfolioGetAllBeers(selectedItemView.getContext()).execute(url);

                }

                else if (portfolioChoice.equals("Brewery")){

                    //inflate brewery spinner
                    //to do: change to start rater
                    LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
                    ll.removeAllViews();

                    //add spinner to layout
                    LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
                    LinearLayout addSpinner = (LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);
                    addSpinner.addView(inflater.inflate(R.layout.addspinner_layout, null));

                    //todo: get breweries and fill spinner


                }








            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);

        return true;
    }




}
公共类公文包扩展ActionBar菜单{
//从bundle获取啤酒详细信息
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_公文包);
最终微调器portfolioType=(微调器)findViewById(R.id.portfolioSpiner);
portfolioType.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
字符串portfolioChoice=portfolioType.getSelectedItem().toString();
Toast.makeText(getApplicationContext(),portfolioChoice,Toast.LENGTH_LONG).show();
Log.d(“投资组合”,portfolioChoice);
如果(投资组合选择等于(“全部”)){
//获取用户ID
//获取用户数据
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(selectedItemView.getContext());
字符串userID=prefs.getString(“userID”,null);
//构造url
String url=“myURL”
//异步任务在这里进行
新PortfolioGetalBeers(selectedItemView.getContext()).execute(url);
}
else if(portfolioChoice.equals(“酿酒厂”)){
//充气酿酒纺纱机
//要执行的操作:更改为开始评分器
LinearLayout ll=(LinearLayout)findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
//将微调器添加到布局
LayoutFlater充气器=(LayoutFlater)选择EditemView.getContext().getSystemService(选择EditemView.getContext().LAYOUT\u充气器\u服务);
LinearLayout addSpinner=(LinearLayout)选择EditemView.getContext().findViewById(R.id.addSpinnerLayout);
addSpinner.addView(充气器.充气(R.layout.addSpinner_layout,null));
//待办事项:去酿酒厂,给纺纱机加满酒
}
}
@凌驾
未选择公共无效(AdapterView父视图){
//你的代码在这里
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main2,菜单);
返回true;
}
}
活动组合:

<?xml version="1.0" encoding="utf-8"?>


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">

                <!-- Card Contents go here -->
                <TextView
                    android:id="@+id/portfolioTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:textSize="20sp"
                    android:textStyle = "bold"
                    android:text="Your Portfolio"
                    android:padding="5dip"
                    >
                </TextView>




            </LinearLayout >

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">

                <!-- Card Contents go here -->
                <TextView
                    android:id="@+id/sortTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:textSize="15sp"
                    android:textStyle = "bold"
                    android:text="Sorting Options:"
                    android:padding="5dip"
                    >
                </TextView>

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    >

                    <Spinner
                        android:id="@+id/portfolioSpinner"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:entries="@array/portfolio_array"
                        android:layout_weight="1"

                        />

                    <LinearLayout
                        android:id="@+id/addSpinnerLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_weight="1">

                    </LinearLayout>


                </LinearLayout>




            </LinearLayout >

        </FrameLayout>

    <ListView
        android:id="@+id/allYourBeersList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="0px"
        android:divider="@null"

        >
    </ListView>


    </LinearLayout>

addspinner\u布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Spinner
        android:id="@+id/portfolioSpinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/portfolio_array"
        android:layout_weight="1"

        />




</LinearLayout>

试试看

LinearLayout addSpinner = inflater.inflate(R.layout.addSpinnerLayout, null);
我对您的语法和尝试执行的操作有点困惑,但这应该可以解决这个问题。

请尝试

LinearLayout addSpinner = inflater.inflate(R.layout.addSpinnerLayout, null);
我对你的语法和你想做什么有点困惑,但这应该可以解决这个问题。

使用这个

  LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.‌​getContext().LAYOUT_INFLATER_SERVICE) 
  View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2 
  ll.addView(v); // add the view to the linear layout
你有这个

<LinearLayout
      android:id="@+id/addSpinnerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1">

 </LinearLayout>
    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout
使布局膨胀

  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
  View v = inflater.inflate(R.layout.addspinner_layout, null);  
然后将其添加到LinearLayout

  ll.addView(v); // add the view to the linear layout
初始化微调器

  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2);
因为你有这个

<LinearLayout
      android:id="@+id/addSpinnerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1">

 </LinearLayout>
    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout
使用此

  LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.‌​getContext().LAYOUT_INFLATER_SERVICE) 
  View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2 
  ll.addView(v); // add the view to the linear layout
你有这个

<LinearLayout
      android:id="@+id/addSpinnerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1">

 </LinearLayout>
    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout
使布局膨胀

  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
  View v = inflater.inflate(R.layout.addspinner_layout, null);  
然后将其添加到LinearLayout

  ll.addView(v); // add the view to the linear layout
初始化微调器

  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2);
因为你有这个

<LinearLayout
      android:id="@+id/addSpinnerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1">

 </LinearLayout>
    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout


您正在膨胀布局在膨胀的布局中初始化线性布局并将其添加到该布局中?我很困惑您是否正在膨胀布局在膨胀的布局中初始化线性布局并将其添加到同一布局中?我很困惑,这给了我一个错误无法解析方法膨胀(?,null)和无法解析方法addSpinnerLayout@Mike你在干什么?你能详细说明一下吗。这令人困惑。另外,在布局中的微调器上发布您的相关xml布局,当我在该微调器中选择一个选项时,我希望旁边显示另一个微调器。@Mike:
selectedItemView
始终是一个视图,因此您是否尝试将其作为
LinearLayout addSpinner=(LinearLayout)selectedItemView.findViewById(R.id.addSpinnerLayout)?这给了我一个错误无法解析方法膨胀(?,null)和无法解析方法addSpinnerLayout@Mike你在干什么?你能详细说明一下吗。这令人困惑。另外,在布局中的微调器上发布您的相关xml布局,当我在该微调器中选择一个选项时,我希望旁边显示另一个微调器。@Mike:
selectedItemView
始终是一个视图,因此您是否尝试将其作为
LinearLayout addSpinner=(LinearLayout)selectedItemView.findViewById(R.id.addSpinnerLayout)?无法解析符号“充气器”是我遇到的错误that@Mike初始化充气机,如
LayoutInflater充气机=(LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT\u充气机\u服务)
检查我添加的编辑,我认为这看起来不错,但是
ll.removeAllViews()需要吗?因为此时,当前膨胀的
布局
activity\u portfolio.xml
,而
addSpinnerLayout的
视图
没有任何children@codeMagic我不认为这是必要的,因为op并没有在线性布局中添加任何内容,而是从他的问题中复制粘贴。编辑thanks@codeMagic令人困惑的是,bcoz的问题并不清楚,op当时也没有发布布局XML。不是你的错无法解决符号“充气器”是我的错误that@Mike初始化充气器lik