什么在这个自定义的错误对话框

问题描述:

我创建一个自定义对话框,将显示一个食品项目的信息: 该对话框的XML布局什么在这个自定义的错误对话框

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/layout_root"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    >   
    <TextView 
    android:id="@+id/lblCal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </TextView> 
    <TextView 
    android:id="@+id/lblFat" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </TextView> 
    <TextView 
    android:id="@+id/lblCarb" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </TextView> 
    <TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
     <Button android:layout_width="wrap_content" 
     android:text=" OK " 
     android:id="@+id/btnOK" 
     android:layout_height="wrap_content"> 
     </Button> 
     <Button android:layout_width="wrap_content" 
     android:text="Home" 
     android:id="@+id/btnHome" 
     android:layout_height="wrap_content"> 
     </Button> 
    </TableRow> 

</LinearLayout> 

的onCreatDialog方法是这样定义的:

@Override 
    protected Dialog onCreateDialog(int id) { 
     Dialog d; 
     switch (id) { 
     case 0: 
      d = new Dialog(this); 
      d.setContentView(R.layout.result_dialog);    
      d.setTitle(item); 
      TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);     
      lblEnergy.setText("Energy: "+ String.valueOf(cal)); 
      TextView lblFat = (TextView) d.findViewById(R.id.lblCal);    
      lblFat.setText("Fat: "+ String.valueOf(fat)); 
      TextView lblCarbs = (TextView) d.findViewById(R.id.lblCal);    
      lblCarbs.setText("Carbs: " + String.valueOf(carb)); 
      Button ok = (Button) d.findViewById(R.id.btnOK);    
      ok.setOnClickListener(i); 
      Button home = (Button) d.findViewById(R.id.btnHome);    
      home.setOnClickListener(i); 
      break; 
      default: d = null; 
     } 
    return d; 
    } 

但是当我跑前两个TextView中不会出现的程序。

是什么问题?!

什么我要补充到OK按钮,关闭对话框,因为当我打电话DissmisDialog(0); 当我在另一个项目的对话框内容从未改变

+0

我也有onClickListener,一个问题,当我写的代码:(情况R.id.btnHome: startActivity(新意图(这一点,Activity_DietTracker.c姑娘));打破;)它给我(这个错误),我可以把getApplicationContext() – Heba 2011-04-02 15:04:45

+0

另一个问题与您的布局:您正在使用的TableRow刚刚冒出:“通过tablerow应始终作为一个孩子TableLayout的“ – 2011-04-02 17:07:00

再次点击您需要使用正确的ID为findViewById电话:

老:

 TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);     
     lblEnergy.setText("Energy: "+ String.valueOf(cal)); 
     TextView lblFat = (TextView) d.findViewById(R.id.lblCal);    
     lblFat.setText("Fat: "+ String.valueOf(fat)); 
     TextView lblCarbs = (TextView) d.findViewById(R.id.lblCal);    
     lblCarbs.setText("Carbs: " + String.valueOf(carb)); 

新:

 TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);     
     lblEnergy.setText("Energy: "+ String.valueOf(cal)); 
     TextView lblFat = (TextView) d.findViewById(R.id.lblFat /* <<< */);    
     lblFat.setText("Fat: "+ String.valueOf(fat)); 
     TextView lblCarbs = (TextView) d.findViewById(R.id.lblCarb /* <<< */);    
     lblCarbs.setText("Carbs: " + String.valueOf(carb));