- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
每当我的 ListView 中有 6 个或更多项时 && 当我尝试单击任何 ListView 行时,我目前都会收到空指针异常。注意:当我单击少于 6 个项目的 ListView 行时,一切都按预期工作。
任何帮助都会很棒我已经在这个问题上停留了很长时间 =\(大约一个月)
12-16 04:11:34.309: W/dalvikvm(2028): threadid=1: thread exiting with uncaught exception (group=0x95b62648)
12-16 04:11:34.309: E/AndroidRuntime(2028): FATAL EXCEPTION: main
12-16 04:11:34.309: E/AndroidRuntime(2028): java.lang.NullPointerException
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
12-16 04:11:34.309: E/AndroidRuntime(2028): at com.android.internal.app.AlertController$AlertParams$1.getView(AlertController.java:879)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.ListView.onMeasure(ListView.java:1159)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 04:11:34.309: E/AndroidRuntime(2028): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.View.measure(View.java:15848)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1079)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.os.Handler.handleCallback(Handler.java:730)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.os.Looper.loop(Looper.java:137)
12-16 04:11:34.309: E/AndroidRuntime(2028): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-16 04:11:34.309: E/AndroidRuntime(2028): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 04:11:34.309: E/AndroidRuntime(2028): at java.lang.reflect.Method.invoke(Method.java:525)
12-16 04:11:34.309: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-16 04:11:34.309: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 04:11:34.309: E/AndroidRuntime(2028): at dalvik.system.NativeStart.main(Native Method)
我的代码我认为我收到此错误的地方是
private class MyListAdapter extends ArrayAdapter<Item>
{
public MyListAdapter(){
//Important 4 SO: customerItems is an arraylist of <Item> Objects
super(UserOrderMenu.this,R.layout.itemview, customerItems);//这是错误的吗?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){ //makes sure we have a view to work with
itemView = getLayoutInflater().inflate(R.layout.itemview,parent,false);
} else{
itemView = convertView;
}
//find the item to work with. using item class and customerItem ArrayList
final Item currentItem = customerItems.get(position);
ImageView imageview = (ImageView) itemView.findViewById(R.id.imageButtonTrashCan);
imageview.setClickable(true);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(currentItem);
notifyDataSetChanged(); //this notifies the Arrayadapter to update
float t1price, t2price, t3price, t4price, t5price;
System.out.println(currentItem.getTopping1Used());
if(currentItem.getTopping1Used()){
t1price = currentItem.getTop1P();
}else
t1price = 0;
if(currentItem.getTopping2Used()){
t2price = currentItem.getTop2P();
}else
t2price =0;
if(currentItem.getTopping3Used()){
t3price = currentItem.getTop3P();
}else
t3price = 0;
if(currentItem.getTopping4Used()){
t4price = currentItem.getTop4P();
}else
t4price =0;
if(currentItem.getTopping5Used()){
t5price = currentItem.getTop5P();
}else
t5price =0;
findTotalPrice(-t1price,-t2price,-t3price,-t4price,-t5price,true);
findTotalPrice(-currentItem.getItemP());
}
});
TextView n = (TextView) itemView.findViewById(R.id.tvItemName);
n.setText(currentItem.getItem().toString());
// then currentItem.getTop1P();
TextView p = (TextView) itemView.findViewById(R.id.tvItemPrice);
p.setText(String.valueOf(currentItem.getItemP()));
return itemView;
}
}//end of MyListAdapter Class
private void populateListView(){ //this is how to add an item to ListView
customerItems.removeAll(Collections.singleton(null)); //remove later
customerItems.removeAll(Arrays.asList(null,""));
ArrayAdapter<Item> adapter;
try{
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}catch(Exception e){
}
}
项目.java:
public class Item {
private String item="none";
private float itemP=0;
private String top1="none";
private float top1P=0;
private String top2="none";
private float top2P=0;
private String top3="none";
private float top3P=0;
private String top4="none";
private float top4P=0;
private String top5="none";
private float top5P=0;
private boolean topping1used = false;
private boolean topping2used = false;
private boolean topping3used = false;
private boolean topping4used = false;
private boolean topping5used = false;
public Item(){
super();
}
public Item(String item, float itemP){
super();
this.item = item;
this.itemP = itemP;
}
public Item(String item, float itemP,String top1,float top1P,String top2,float top2P,String top3,
float top3P,String top4,float top4P,String top5,float top5P)
{
super();
this.item = item;
this.itemP = itemP;
//this is a price dont have to worry about it
this.top1 = top1;
this.top1P = top1P;
this.top2 = top2;
this.top2P = top2P;
this.top3 = top3;
this.top3P = top3P;
this.top4 = top4;
this.top4P = top4P;
this.top5 = top5;
this.top5P = top5P;
}
public Item(boolean t1, boolean t2, boolean t3, boolean t4, boolean t5){
super();
this.topping1used = t1;
this.topping2used = t2;
this.topping3used = t3;
this.topping4used = t4;
this.topping5used = t5;
}
//set
public void setTopping1used(boolean topping1used) {
this.topping1used = topping1used;
}
public void setTopping2used(boolean topping2used) {
this.topping2used = topping2used;
}
public void setTopping3used(boolean topping3used) {
this.topping3used = topping3used;
}
public void setTopping4used(boolean topping4used) {
this.topping4used = topping4used;
}
public void setTopping5used(boolean topping5used) {
this.topping5used = topping5used;
}
//get
public String getItem() {
return item;
}
public float getItemP() {
return itemP;
}
public String getTop1() {
return top1;
}
public float getTop1P() {
return top1P;
}
public String getTop2() {
return top2;
}
public float getTop2P() {
return top2P;
}
public String getTop3() {
return top3;
}
public float getTop3P() {
return top3P;
}
public String getTop4() {
return top4;
}
public float getTop4P() {
return top4P;
}
public String getTop5() {
return top5;
}
public float getTop5P() {
return top5P;
}
public boolean getTopping1Used(){
return topping1used;
}
public boolean getTopping2Used(){
return topping2used;
}
public boolean getTopping3Used(){
return topping3used;
}
public boolean getTopping4Used(){
return topping4used;
}
public boolean getTopping5Used(){
return topping5used;
}
}//end of Item class
private void populateList(String item, float price,String t1n, float t1p, String t2n, float t2p,
String t3n,float t3p, String t4n, float t4p, String t5n, float t5p)
{
customerItems.add(new Item(item,price,t1n,t1p,t2n,t2p,t3n,t3p,t4n,t4p,t5n,t5p));
populateListView();
}
private void populateListView(){//这是将项目添加到 ListView 的方法 customerItems.removeAll(Collections.singleton(null));//稍后删除 customerItems.removeAll(Arrays.asList(null,"")); ArrayAdapter适配器;
try{
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}catch(Exception e){
}
}
最佳答案
1)你的编码标准不好,我建议你改进一下
2) 你没有正确提到问题,你粘贴的代码有太多评论,因为很难知道你想要实现什么,或者你故意评论粘贴 SO。
3) 你的getView()方法实现有误,请检查以下正确方法:
if (convertView == null)
{
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mLayoutInflater.inflate(R.layout.Your_Row_XML_File, null);
viewHolder = new ViewHolder();
viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1_in_row_xml_inflated_as_above);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView1.setText("Any string value");
而您的 ViewHolder 在您的 xml 文件中包含您的 View 的 View 声明:
private final class ViewHolder
{
public TextView textView1;
public TextView textView2; // if any and add views like this
}
引用示例和 Android 文档以明确理解,这会很好,并为您的代码奠定良好的基础。
希望对您有所帮助。
关于android - ListView NullPointerException @ ArrayAdapter.createViewFromResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607826/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!