【问题标题】:ListView crashes. Not working app. Android Studio EmulatorListView 崩溃。不工作的应用程序。 Android Studio 模拟器
【发布时间】:2018-03-01 11:54:50
【问题描述】:

我构建了一个 ListView,当我尝试运行该应用程序时,它一直说它不会加载。它需要关闭。所以我无法在 Android Studio 虚拟设备上运行它。

import android.content.res.Resources;


    public class MainActivity4 extends AppCompatActivity {


    ListView myListView;

    String [] items;

    String [] prices;
    String [] descriptions;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);


        Resources res = getResources();

        myListView = (ListView) findViewById(R.id.myListView);


        items = res.getStringArray(R.array.items);


               prices = res.getStringArray(R.array.prices);
        descriptions = res.getStringArray(R.array.descriptions);


        ItemAdapter itemAdapter = new ItemAdapter(this, items, prices,descriptions);


        myListView.setAdapter(itemAdapter);


    }
}

我还创建了一个 ItemAdapter 类,它继承自 BaseAdapter 以扩展布局。

物品适配器代码

    import android.content.Context;
    import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    public class ItemAdapter extends BaseAdapter {

    LayoutInflater mInflater;
    //String array of the items
    String [] items;
    //reference to the other arrays
    String [] prices;
    String [] descriptions;

    //constructor for this class

    public ItemAdapter(Context c, String[] items, String[] prices, String [] descriptions){
    items =items;
    prices=prices;
    decriptions=descriptions

      mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    }




    @Override

    //how many items are in this list
    public int getCount() {
    return items.length;
    }

    @Override
    //Get an item from the list
    public Object getItem(int i) {
    return items[i];
    }

    @Override
    //Get only the index of the item
    public long getItemId(int i) {
        return i;
    }

    @Override


    public View getView(int i, View view, ViewGroup viewGroup) {



        View v = mInflater.inflate(R.layout.my_list,null);

        TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
        TextView descriptionTextView = (TextView) v.findViewById(R.id.descriptionTextView);
        TextView priceTextView = (TextView) v.findViewById(R.id.priceTextView);


        String name = items[i];
        String desc = descriptions[i];
        String cost = prices[i];

        //now we put the information on the textview

        nameTextView.setText(name);
        descriptionTextView.setText(desc);
        priceTextView.setText(cost);

        return v;

        //within that layout file there's going to be 3 TextViews to put in 3 strings

    }


}

我也对Resources下的String文件做了一些改动

<resources>
<string name="app_name">Lists</string>
<string-array name="items">
    <item>Olives</item>
    <item>Tomato</item>
    <item>Apple</item>
</string-array>

<string-array name="Prices">
    <item>$0.99</item>
    <item>$0.49</item>
    <item>$.89</item>
</string-array>

<string-array name="descriptions">
    <item>Fresh Olives from Saranda</item>
    <item>Fresh tomatoes from Lushnja</item>
    <item>Fresh apples from Korça </item>
</string-array>

当我运行应用程序时,模拟器一直说程序已停止。

怎么了?

【问题讨论】:

  • 可以添加崩溃的堆栈跟踪吗?
  • 请注意,列表视图是老派。尝试回收站视图。 ;)

标签: android android-layout listview android-studio layout-inflater


【解决方案1】:

您的字符串数组价格用大写字母书写,但您使用小写字母访问。

<string-array name="Prices">
    <item>$0.99</item>
    <item>$0.49</item>
    <item>$.89</item>
</string-array>

 prices = res.getStringArray(R.array.prices);

试试这样:

<string-array name="prices">
    <item>$0.99</item>
    <item>$0.49</item>
    <item>$.89</item>
</string-array>

编辑

您应该注意的其他内容在您的适配器中:

 public ItemAdapter(Context c, String[] items, String[] prices, String [] descriptions){
    this.items =items;
     this.prices=prices;
     this.decriptions=descriptions

      mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

如果您不使用this,您将不会从活动中传递数组,因此您的数组将不会被初始化。在getView() 方法中,当您尝试访问它们时,您将获得 NPE。

【讨论】:

  • 完美通知+1
  • 对不起,我在复制代码时出错了。它是小写字母。我在输入或引用时没有任何错误。这个想法是,当我运行它时它不起作用。一直停。
【解决方案2】:

你的代码是这样的:

items = res.getStringArray(R.array.items);


           prices = res.getStringArray(R.array.prices);
    descriptions = res.getStringArray(R.array.descriptions);


    ItemAdapter itemAdapter = new ItemAdapter(this, items, prices,descriptions);

我想你可以在这里试试这个:

items[] = res.getStringArray(R.array.items);
prices[] = res.getStringArray(R.array.prices);
descriptions[] = res.getStringArray(R.array.descriptions);


    ItemAdapter itemAdapter = new ItemAdapter(this, items[], prices[], descriptions[]);

看看它是如何工作的。

【讨论】:

  • @Inter_forever 尝试在您的 ItemAdaptor 构造函数中更改它 items[] =items[];价格[]=价格[]; descriptions[]=descriptions[];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 2011-03-20
  • 2017-10-27
  • 1970-01-01
  • 2019-05-19
相关资源
最近更新 更多