ListView崩溃。不工作的应用程序Android Studio模拟器

【ListView崩溃。不工作的应用程序Android Studio模拟器】少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述ListView崩溃。不工作的应用程序Android Studio模拟器相关的知识,希望能为你提供帮助。
我构建了一个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 classpublic ItemAdapter(Context c, String[] items, String[] prices, String [] descriptions){ items =items; prices=prices; decriptions=descriptionsmInflater = (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; }@Overridepublic 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 textviewnameTextView.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>


当我运行应用程序时,模拟器一直说该程序已停止。
怎么了?
答案您的字符串数组价格是用大写字母写的,但您使用小写字母访问。
< 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=descriptionsmInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }

如果不使用this,则不会从活动中传递数组,因此不会初始化数组。在getView()方法中,当您尝试访问它们时,您将获得NPE。
另一答案你的代码是这样的:
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[]);

看看它将如何运作。

    推荐阅读