我有一个微调器,在选中时应该向视图中添加一个新的微调器。但是当我尝试膨胀时,我在这一行上收到错误:
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;
}
}
activity_portfolio:
<?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_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>
答案 0 :(得分:2)
尝试改为
LinearLayout addSpinner = inflater.inflate(R.layout.addSpinnerLayout, null);
我对你的语法和你想要做的事情感到有些困惑,但这应该可以解决这个问题。
答案 1 :(得分: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
Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2);
因为你有这个
<Spinner
android:id="@+id/portfolioSpinner2" // in addSpinner_layout