Android 什么';这个自定义对话框中有什么错误

Android 什么';这个自定义对话框中有什么错误,android,dialog,Android,Dialog,我正在创建一个自定义对话框,显示食品的信息: 对话框的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:

我正在创建一个自定义对话框,显示食品的信息: 对话框的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>
但是当我运行程序时,前两个文本视图不会出现

有什么问题

我应该向ok按钮添加什么来关闭对话框,因为当我调用DissmisDialog(0)时;
当我再次单击另一项时,对话框内容从未更改

您需要为
findViewById
调用使用正确的ID:

旧的:

新的:

TextView lblEnergy=(TextView)d.findViewById(R.id.lblCal);
lblEnergy.setText(“能量:+String.valueOf(cal));

TextView lblFat=(TextView)d.findViewById(R.id.lblFat/*我在编写代码时,onClickListener也有问题:(case R.id.btnHome:startActivity(new Intent(this,Activity_dietracker.class));break;)它给我(this)一个错误,我能把getApplicationContext()放进去吗布局中的另一个问题:您正在使用一个表行,这是无中生有的:“表行应始终用作表布局的子级”
@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 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));