0

I have a spinner that when selected should add a new spinner to the view. 但是当我尝试充气时,我在这一行得到一个错误:

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;
    }




}

活动组合:

<?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>

添加pinner_layout:

<?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>
4

2 回答 2

2

试试吧

LinearLayout addSpinner = inflater.inflate(R.layout.addSpinnerLayout, null);

我对你的语法和你正在尝试做的事情有点困惑,但这应该可以解决这个问题。

于 2013-10-13T04:22:09.913 回答
2

用这个

  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>

我想您需要将微调器添加到线性布局中。

所以

 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);  

然后将其添加到 LinearLayout

  ll.addView(v); // add the view to the linear layout

初始化微调器

  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2);

因为你有这个

    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout
于 2013-10-13T04:34:24.087 回答