ListView崩溃。不工作的应用程序Android Studio模拟器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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>
当我运行应用程序时,模拟器一直说该程序已停止。
怎么了?
答案
您的字符串数组价格是用大写字母写的,但您使用小写字母访问。
<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。
另一答案
你的代码是这样的:
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[]);
看看它将如何运作。
以上是关于ListView崩溃。不工作的应用程序Android Studio模拟器的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?
Androi:ListView+GridView实现仿微信微博朋友圈无焦点冲突
没有@android:id/text1,Listview 布局将无法工作
单个 ListView 内的多个 ViewPagers 崩溃
自定义 listView 数组,搜索,在 NotifyDataSetChanged 上崩溃,加上 listView 没有得到更新