我正在尝试设置一个简单的文本旋转器来显示我的产品尺寸,但我的虚拟机一直关闭。我已经做了详细的断点分析,所以请不要去那里。您对为什么这不起作用有何看法?
11-20 01:07:23.333 11588-11588/com.app.store D/AndroidRuntime﹕ Shutting down VM
我将发布我的整个 fragment 。您会注意到我正在尝试将 String[] 数组输入适配器中。我有一个明确定义的和一个注释掉的,我是从另一个类中提取的。这两种方法都不起作用。
public class BuyFragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_buy, container, false);
view = rootView;
return rootView;
}
public int GetProductSizeCode() {
return ((Spinner) view.findViewById(R.id.spinner_sizes)).getSelectedItemPosition();
}
public void DisplayProduct(Product product) throws ParseException {
String productName = product.getName();
String productCost = "USD $" + product.getCost();
String productDescription = product.getDescription();
String careInstructions = product.getCareInstructions();
// String [] productSizes = product.getSizes();
String [] productSizes = new String[] {"1", "2", "3"};
setUpSpinner(productSizes);
((TextView) view.findViewById(R.id.productTitle)).setText(productName);
((TextView) view.findViewById(R.id.productCost)).setText(productCost);
((TextView) view.findViewById(R.id.productDescription)).setText(productDescription);
((TextView) view.findViewById(R.id.careInstructions)).setText(careInstructions);
((ImageView) view.findViewById(R.id.productImage)).setImageResource(product.getTemplateDrawableOverlay());
}
public void setUpSpinner(String[] productSizes) {
Spinner spnr;
spnr = (Spinner)view.findViewById(R.id.spinner_sizes);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
R.layout.custom_spinner,
R.id.spinner_sizes,
productSizes);
spnr.setAdapter(adapter);
}
}
这是我的自定义微调器,但您可能不需要它:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@color/elgamigrey">
<TextView
android:id="@+id/text_main_seen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select your size"
android:fontFamily="sans-serif-light"
android:textSize="18sp"
android:textColor="#fff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
有人知道发生了什么事吗?尽管可能发生其他情况,代码至少是正确的吗?
更新:
我的资源 ID 和布局顺序错误。设置微调器时应该是这样的:
R.layout.custom_spinner,
R.id.spinner_sizes,
最佳答案
根据documentation ,当您创建 ArrayAdapter
时,第三个参数应该是要在其中呈现 Spinner
内容的 TextView
的 id。因此,查看您的 XML 布局,id 为 R.id.text_main_seen
,因此适配器的创建应类似于:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
R.layout.custom_spinner,
R.id.text_main_seen,
productSizes);
关于java - 尝试设置 TextSpinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822841/