将数据加载到自定义文本微调器后,VM关闭

时间:2015-11-20 04:59:13

标签: java android class android-spinner

我有一个片段,向用户显示他们从上一个屏幕中选择的产品。我通过一个单独的CustomProduct类将不同的产品加载到它中,该类基本上为这个片段中显示的产品赋值。它将显示名称,标题,成本,描述等内容。

我遇到问题的部分是检索产品大小的String []数组,即Small,Medium,Large等。所有这些都使用显式的String []数组,即String [] spinnerValues = {1,2,3};但是一旦我尝试使它动态化,即String [] spinnerValues = product.getSizes();我尝试打开片段,我的虚拟机关闭了。它没有给我一个错误或任何东西。

public void DisplayProduct(CustomProduct product) throws ParseException {

    String productName = product.getName();
    String productCost = "USD $" + product.getCost();
    String productDescription = product.getDescription();
    String careInstructions = product.getCareInstructions();

    String[] spinnerValues = product.getSizes();
    Spinner mySpinner = (Spinner) view.findViewById(R.id.spinner_sizes);
    mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, R.id.text_main_seen, spinnerValues));

    ((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 class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(BuyFragment ctx, int custom_spinner, int txtViewResourceId, String[] spinnerValues) {
        super(ctx.getActivity(), txtViewResourceId, custom_spinner, spinnerValues);
    }

    public View getDropDownView(int position, View cnvtView, ViewGroup prnt, String[] spinnerValues) {
        return getCustomView(position, cnvtView, prnt, spinnerValues);
    }

    public View getView(int pos, View cnvtView, ViewGroup prnt, String[] spinnerValues) {
        return getCustomView(pos, cnvtView, prnt, spinnerValues);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent, String[] spinnerValues) {
        LayoutInflater inflater = getLayoutInflater(getArguments());
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
        TextView main_text = (TextView)mySpinner.findViewById(R.id.text_main_seen);
        main_text.setText(spinnerValues[position]);
        return mySpinner;
    }
}

我有一个单独的类,我们称之为&#34; Shirt&#34;,用于检索包含给定产品大小的String []数组。

@Override
public String[] getSizes() {
    return new String[] { "XS", "S", "M", "L", "XL", "2XL" };
}

你可以看到我要去哪里。我将根据产品类型获得不同的尺寸参数,并且这些参数恰好是扩展CustomProduct的单独类别,即&#34; Leggings&#34;下面:

@Override
public String[] getSizes() {
    return new String[] { "XS", "S", "M", "L" };
}

任何人都知道会发生什么事吗?问题肯定出在MyAdapter的某个地方。

编辑:Logcat

11-19 22:26:59.366  12576-12576/com.app.store D/AndroidRuntime﹕ Shutting down VM

在mySpinner.setAdapter设置断点(new MyAdapter(this,R.layout.custom_spinner,R.id.text_main_seen));

这就是我得到的:

this = {com.app.customizer.BuyFragment@5075}"BuyFragment{aedb146 #3 id=0x7f0b0091 android:switcher:2131427473:3}"
product = {com.app.products.Shirt@5084}
productName = {java.lang.String@5085}"American Apparel Unisex All-Over T-Shirt"
productCost = {java.lang.String@5086}"USD $36"
productDescription = {java.lang.String@5087}"Our all-over t-shirts are printed on the highest quality Unisex Polyester Jersey we could get our hands on (100% Polyester). Your all-over t-shirt will be printed identically on both sides. Made in USA."
careInstructions = {java.lang.String@5088}"Machine wash cold, gentle cycle with like colors."
mySpinner = {android.support.v7.widget.AppCompatSpinner@5089}"android.support.v7.widget.AppCompatSpinner{67ee51b V.ED..C.. ......ID 48,726-1032,726 #7f0b0103 app:id/spinner_sizes}"
view = {android.widget.ScrollView@5090}"android.widget.ScrollView{bb7f6b8 VFED.V... ........ 2160,0-3240,1554}"

解决方案:

我设置了一个旋转器旋转器,它似乎工作。只需要确保我的资源ID和布局xml的顺序正确:

Trying to Set up TextSpinner

0 个答案:

没有答案