更新挑战

时间:2014-08-05 13:19:05

标签: android android-listview

我制作了一个列表视图,其中包含每种成分及其名称的所有卡路里。当用户使用按钮及其在customlv类中指定的onclick方法改变自定义列表视图时,即可更新主类中的Button文本。

在我的作业问题中,用户可以更改改变相应卡路里的成分。例如,鸡蛋(500卡路里)到菠菜(100卡路里)。其中100卡路里将更新到主类的textview。

我在第79行有空指针的错误,我指的是

这是我的代码:

主要课程:

 public void recalculate(int sum) {
    Button recal;
     recal = (Button)findViewById(R.id.totalCalories);
    recal.setText("Total Calories : "+ sum);

 }

主要类:

    public IngredientCal(){}

customLv:ic1在此处设置:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

    LayoutInflater inflate = context.getLayoutInflater();
    View rowView1 = inflate.inflate(R.layout.list_single_two, null, true);

    final TextView ingredients1 = (TextView)rowView1.findViewById(R.id.ingredienttv);
    final TextView amt1 = (TextView)rowView1.findViewById(R.id.amounttv);
    final TextView cal1 = (TextView)rowView1.findViewById(R.id.caltv1);
    final Button alternate = (Button)rowView1.findViewById(R.id.alternate);
    final IngredientCal ic1 = new IngredientCal();

customlv :(在Click on click方法中) 获取查看方法

                @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflate = context.getLayoutInflater();
    View rowView1 = inflate.inflate(R.layout.list_single_two, null, true);

    final TextView ingredients1 = (TextView)rowView1.findViewById(R.id.ingredienttv);
    final TextView amt1 = (TextView)rowView1.findViewById(R.id.amounttv);
    final TextView cal1 = (TextView)rowView1.findViewById(R.id.caltv1);
    final Button alternate = (Button)rowView1.findViewById(R.id.alternate);
    final IngredientCal ic1 = new IngredientCal();
    ingredients1.setText(""+ingredients.get(position));
    cal1.setText(""+cal.get(position));
    amt1.setText(""+amt.get(position));

    // alternate ingredient button
    if(alt.get(position) == 1){


        alternate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //
                if(x == 1){

                alternate.setText("Bck");

                String a = AltIngredients.get(position);
                String b = ""+calA.get(position);
                ingredients1.setText(""+a);
                cal1.setText(""+b);


                //add new ingredient,
                ingredients.remove(position);
                ingredients.add(position, AltIngredients.get(position));

                cal.remove(position);
                cal.add(position, calA.get(position));

                for(int i =0; i <cal.size(); i++){
                    sum += cal.get(i);

                    Log.i("SUM", ""+ cal.get(i) );
                }
                ic1.recalculate(sum);
                x--;

                }else{

                    alternate.setText("Alt");   

                    String a = ingredients.get(position);
                    String b = ""+cal.get(position);
                    ingredients1.setText(""+a);
                    cal1.setText(""+b);
                    x++;
                }
            }


        });




    }else{

        alternate.setEnabled(false);

    }



    return rowView1;





}

主类的xml:

    <Button
        android:id="@+id/totalCalories"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Total Calories : "
        android:textSize="25dp"
        android:padding="7dp"
        android:background="@drawable/total"
        android:layout_gravity="center"
        android:gravity="center"
        />

1 个答案:

答案 0 :(得分:0)

NPE(空指针异常)是由变量ic1为NULL引起的。

getView方法的内容中,我无法看到行ic1.recalculate(sum);会抛出NPE。因为ic1onClick处理程序注册之前已经清楚地初始化了。我对sum变量感到好奇,我希望在onClick方法中声明它,但它不会在任何地方声明。

您能告诉我们sum的声明吗?并使用您正在接收的完整空指针异常跟踪更新您的问题。