2013-03-06 122 views
0

我一直在尋找解決方案的幾個小時。我正在嘗試構建BodyMassIndex計算器。我已經完成了該程序(我認爲),但每次嘗試運行應用程序時,它都會在模擬器上崩潰。最重要的是,佈局預覽不再顯示。我仍然試圖瞭解日誌中的所有錯誤代碼。我認爲這是我的layout.xml的問題,但我不確定。任何建議或意見將不勝感激!致命例外:main&NullPointerException

logcat的

03-06 06:04:56.533: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:56.533: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:56.533: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:56.853: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:56.853: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.623: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.623: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.633: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.673: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.673: W/Trace(969): Unexpected value from nativeGetEnabledTags: 0 
03-06 06:04:57.683: D/AndroidRuntime(969): Shutting down VM 
03-06 06:04:57.683: W/dalvikvm(969): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 
03-06 06:04:57.703: E/AndroidRuntime(969): FATAL EXCEPTION: main 
03-06 06:04:57.703: E/AndroidRuntime(969): java.lang.NullPointerException 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1348) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1336) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.TableRow.onMeasure(TableRow.java:114) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.View.measure(View.java:15513) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.TableLayout.measureVertical(TableLayout.java:476) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.TableLayout.onMeasure(TableLayout.java:439) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.View.measure(View.java:15513) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.View.measure(View.java:15513) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.View.measure(View.java:15513) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
03-06 06:04:57.703: E/AndroidRuntime(969): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.View.measure(View.java:15513) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.Choreographer.doFrame(Choreographer.java:532) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.os.Handler.handleCallback(Handler.java:725) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.os.Looper.loop(Looper.java:137) 
03-06 06:04:57.703: E/AndroidRuntime(969): at android.app.ActivityThread.main(ActivityThread.java:5039) 
03-06 06:04:57.703: E/AndroidRuntime(969): at java.lang.reflect.Method.invokeNative(Native Method) 
03-06 06:04:57.703: E/AndroidRuntime(969): at java.lang.reflect.Method.invoke(Method.java:511) 
03-06 06:04:57.703: E/AndroidRuntime(969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-06 06:04:57.703: E/AndroidRuntime(969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-06 06:04:57.703: E/AndroidRuntime(969): at dalvik.system.NativeStart.main(Native Method) 
03-06 06:04:57.833: D/dalvikvm(969): GC_CONCURRENT freed 155K, 10% free 2670K/2944K, paused 16ms+3ms, total 139ms 

Java代碼

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class BMICalc extends Activity { 

    // Constants Used when saving/restoring state. 
    private static final String FEET_OF_HEIGHT = "FEET_OF_HEIGHT"; 
    private static final String INCH_OF_HEIGHT = "INCH_OF_HEIGHT"; 
    private static final String WEIGHT_IN_LBS = "WEIGHT_IN_LBS"; 

    private int currentFeetPortionOfHeight;   // Feet portion of height. 
    private int currentInchesPortionOfHeight;  // Inches portion of height. 
    private double currentWeight;     // Weight of user in pounds. 
    private EditText feetPortionOfHeightEditText; // Displays feet portion of height. 
    private EditText inchesPortionOfHeightEditText; // Displays inches portion of height. 
    private EditText weightEditText;    // Displays weight of user. 
    private Button calculateButton;     // Button that starts calculation. 
    private TextView bmiResultTextView;    // Displays BMI value. 
    private TextView bmiResultCommentsTextView;  // Displays comment on user's BMI result. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bmicalc); 

     if (savedInstanceState == null) { // If the application just started... 
      currentFeetPortionOfHeight = 0;  // Initialize the feet portion to 0. 
      currentInchesPortionOfHeight = 0; // Initialize the inches portion to 0. 
      currentWeight = 0.0;    // Initialize the weight to 0. 
      // calculateBMI();     // Calculation for BMI value. 

     } 
     else { // Else (the application is being recovered from memory)... 
      currentFeetPortionOfHeight = savedInstanceState.getInt(FEET_OF_HEIGHT);  // Initialize the feet portion to saved value. 
      currentInchesPortionOfHeight = savedInstanceState.getInt(INCH_OF_HEIGHT); // Initialize the inches portion to saved value. 
      currentWeight = savedInstanceState.getDouble(WEIGHT_IN_LBS);    // Initialize the weight to saved value. 
      // calculateBMI();               // Calculation for BMI value. 
     } 

     // Get references to feet/inches/weight EditTexts. 
     feetPortionOfHeightEditText = (EditText) findViewById(R.id.feetPortionOfHeightEditText); 
     inchesPortionOfHeightEditText = (EditText) findViewById(R.id.inchesPortionOfHeightEditText); 
     weightEditText = (EditText) findViewById(R.id.weightEditText); 

     // Get references to TextViews displaying BMI & comment. 
     bmiResultTextView = (TextView) findViewById(R.id.bmiResultTextView); 
     bmiResultCommentsTextView = (TextView) findViewById(R.id.bmiResultCommentsTextView); 

     // Get reference to calculateButton. 
     calculateButton = (Button) findViewById(R.id.calculateButton); 

     // EditText Watchers handle the EditTexts onTextChanged events. 
     feetPortionOfHeightEditText.addTextChangedListener(feetPortionOfHeightEditTextWatcher); 
     inchesPortionOfHeightEditText.addTextChangedListener(inchesPortionOfHeightEditTextWatcher); 
     weightEditText.addTextChangedListener(weightEditTextWatcher); 
     calculateButton.setOnClickListener(calculateButtonWatcher); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_bmicalc, menu); 
     return true; 
    } 

    protected void onSavedInstanceState (Bundle outState) { 

     // Saves the values of feetPortion.., inchesPortion.., & weightEditText. 
     super.onSaveInstanceState(outState); 
     outState.putDouble(FEET_OF_HEIGHT, currentFeetPortionOfHeight); 
     outState.putDouble(INCH_OF_HEIGHT, currentInchesPortionOfHeight); 
     outState.putDouble(WEIGHT_IN_LBS, currentWeight); 
    } 

    private void calculateBMI() { 

     // Calculates the bmiResult. 
     double bmiResult = currentWeight * 703/
       Math.pow((currentFeetPortionOfHeight * 12 + currentInchesPortionOfHeight), 2); 

     // Sets the bmiResultComment statement based on bmiResult value. 
     String bmiResultComment = setBMIResultComment(bmiResult); 

     // Sets bmiResultTextView & bmiResultCommentTextView. 
     bmiResultTextView.setText(String.format("%.02f", bmiResult)); 
     bmiResultCommentsTextView.setText(bmiResultComment); 
    } 

    private String setBMIResultComment (double bmiResult) { 
     String bmiResultComment = ""; // Creates & initializes the bmiResult comment statement. 

     // Sets the bmiResultComment to appropriate statement. 
     if (bmiResult == 0.0) { 

      bmiResultComment = ""; // If BMI is zero, nothing has been entered & nothing needs to be displayed. 

     } else if (bmiResult < 18.5) { 

      bmiResultComment = "BMI Values under 18.5 are considered to be UNDERWEIGHT!"; 

     } else if (bmiResult < 24.9) { 

      bmiResultComment = "BMI Values between 18.5 and 24.9 are considered to be NORMAL."; 

     } else if (bmiResult < 29.9) { 

      bmiResultComment = "BMI Values between 24.9 and 29.9 are considered to be OVERWEIGHT!"; 

     } else { 

      bmiResultComment = "BMI Values over 30 are considered to be OBESE!!!"; 

     } 

     return bmiResultComment; // Returns the bmiResultComment string value. 
    } 

    private OnClickListener calculateButtonWatcher = new View.OnClickListener() { 

     @Override 
     public void onClick(View calculateButtonListener) { 
      calculateBMI(); // Calculation for BMI value. 
     } 

    }; 

    private TextWatcher feetPortionOfHeightEditTextWatcher = new TextWatcher() { 

     @Override 
     public void onTextChanged (CharSequence s, int start, int before, int count) { 

      try {currentFeetPortionOfHeight = (int) Double.parseDouble(s.toString());} // Convert currentFeetPortion... to double. 
      catch (NumberFormatException e) {currentFeetPortionOfHeight = 0;}   // Default if an exception occurs. 
     } 
     @Override 
     public void beforeTextChanged (CharSequence s, int start, int before, int count) { /* Intentionally left empty. */ } 
     @Override 
     public void afterTextChanged (Editable s) { /* Intentionally left empty. */ } 
    }; 

    private TextWatcher inchesPortionOfHeightEditTextWatcher = new TextWatcher() { 

     @Override 
     public void onTextChanged (CharSequence s, int start, int before, int count) { 

      try {currentInchesPortionOfHeight = (int) Double.parseDouble(s.toString());} // Convert currentInchesPortion... to double. 
      catch (NumberFormatException e) {currentInchesPortionOfHeight = 0;}   // Default if an exception occurs. 
     } 
     @Override 
     public void beforeTextChanged (CharSequence s, int start, int before, int count) { /* Intentionally left empty. */ } 
     @Override 
     public void afterTextChanged (Editable s) { /* Intentionally left empty. */ } 
    }; 

    private TextWatcher weightEditTextWatcher = new TextWatcher() { 

     @Override 
     public void onTextChanged (CharSequence s, int start, int before, int count) { 

      try {currentWeight = (int) Double.parseDouble(s.toString());} // Convert currentWeight to double. 
      catch (NumberFormatException e) {currentWeight = 0;}   // Default if an exception occurs. 
     } 
     @Override 
     public void beforeTextChanged (CharSequence s, int start, int before, int count) { /* Intentionally left empty. */ } 
     @Override 
     public void afterTextChanged (Editable s) { /* Intentionally left empty. */ } 
    }; 

} 

佈局XML

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/TableLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="left" 
     tools:context=".BMICalc" > 

     <TableRow 
      android:id="@+id/tableRow0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/introductionTextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_column="0" 
       android:layout_span="2" 
       android:layout_weight="1" 
       android:background="@color/DodgerBlue" 
       android:gravity="center" 
       android:padding="@dimen/introductionTextViewPadding" 
       android:text="@string/introductionTextView" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="@color/White" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/heightPromptTextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_column="0" 
       android:layout_span="2" 
       android:layout_weight="1" 
       android:padding="5sp" 
       android:background="@color/LightGrey" 
       android:text="@string/heightPromptTextView" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" > 

      <EditText 
       android:id="@+id/feetPortionOfHeightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number" /> 

      <TextView 
       android:id="@+id/feetPortionOfHeightViewText" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="0" 
       android:gravity="center_vertical" 
       android:padding="5sp" 
       android:paddingRight="5sp" 
       android:text="@string/feetPortionOfHeightTextView" /> 

      <EditText 
       android:id="@+id/inchesPortionOfHeightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number" /> 

      <TextView 
       android:id="@+id/inchesPortionOfHeightViewText" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="0" 
       android:gravity="center_vertical" 
       android:padding="5sp" 
       android:paddingRight="5sp" 
       android:text="@string/inchPortionOfHeightTextView" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/weightPromptTextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_column="0" 
       android:layout_span="2" 
       android:layout_weight="1" 
       android:padding="5sp" 
       android:background="@color/LightGrey" 
       android:text="@string/weightPromptTextView" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" 
      android:paddingLeft="20sp" > 

      <EditText 
       android:id="@+id/weightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:layout_column="1" 
       android:inputType="number" > 

       <requestFocus /> 
      </EditText> 

      <TextView 
       android:id="@+id/weightTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:gravity="center|left" 
       android:padding="5sp" 
       android:text="@string/weightTextView" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" > 

      <Button 
       android:id="@+id/calculateButton" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:textColor="@color/White" 
       android:textStyle="bold" 
       android:background="@color/Navy" 
       android:text="@string/calculateButton" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" > 

      <TextView 
       android:id="@+id/bmiResultTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="@string/bmiResultsTextView" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/bmiResultCommentsTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="@string/bmiResultCommentsTextView" /> 

     </TableRow> 

    </TableLayout> 
+1

['NullPointerException'(http://developer.android.com/reference/java/lang/NullPointerException.html) – 2013-03-06 06:54:17

回答

0

有一些親在佈局文件中出現瑕疵。跨越錶行的錯誤和子高度。 嘗試使用此xml。只需複製表格row0粘貼相同,它應該工作。

<TableRow 
     android:id="@+id/tableRow0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/introductionTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 

      android:layout_weight="1" 
      android:background="#00ff00" 
      android:gravity="center" 
      android:padding="10dp" 
      android:text="introductionTextView" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#ffffff" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/heightPromptTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 

      android:layout_weight="1" 
      android:padding="5sp" 
      android:background="#aaaaaa" 
      android:text="heightPromptTextView" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5sp" > 

     <EditText 
      android:id="@+id/feetPortionOfHeightEditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="number" /> 

     <TextView 
      android:id="@+id/feetPortionOfHeightViewText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:gravity="center_vertical" 
      android:padding="5sp" 
      android:paddingRight="5sp" 
      android:text="feetPortionOfHeightTextView" /> 

     <EditText 
      android:id="@+id/inchesPortionOfHeightEditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="number" /> 

     <TextView 
      android:id="@+id/inchesPortionOfHeightViewText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:gravity="center_vertical" 
      android:padding="5sp" 
      android:paddingRight="5sp" 
      android:text="inchPortionOfHeightTextView" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/weightPromptTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 

      android:layout_weight="1" 
      android:padding="5sp" 
      android:background="#aaaaaa" 
      android:text="weightPromptTextView" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5sp" 
     android:paddingLeft="20sp" > 

     <EditText 
      android:id="@+id/weightEditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_column="1" 
      android:inputType="number" > 

      <requestFocus /> 
     </EditText> 

     <TextView 
      android:id="@+id/weightTextView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center|left" 
      android:padding="5sp" 
      android:text="weightTextView" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5sp" > 

     <Button 
      android:id="@+id/calculateButton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:textColor="#ffffff" 
      android:textStyle="bold" 
      android:background="#00ff00" 
      android:text="calculateButton" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5sp" > 

     <TextView 
      android:id="@+id/bmiResultTextView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="bmiResultsTextView" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/bmiResultCommentsTextView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="bmiResultCommentsTextView" /> 

    </TableRow> 

</TableLayout> 
+0

謝謝你們這麼多!那就是訣竅。現在我只需要在點擊EditText時彈出鍵盤。 – 2013-03-06 14:54:12

1

請記住以下關於TableLayout。

水平排列其子項的佈局。 TableRow應始終用作TableLayout的子項。如果TableRow的父級不是TableLayout,則TableRow將表現爲水平LinearLayout。

TableRow的子元素不需要在XML文件中指定layout_width和layout_height屬性。 TableRow總是強制這些值分別爲MATCH_PARENT和WRAP_CONTENT。

我用你的佈局和刪除了一些屬性。它現在工作。您可以做一件事,嘗試逐個添加已刪除的屬性,並使用圖形佈局選項卡檢查有效性。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/TableLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="left" 
     tools:context=".MainActivity" > 

     <TableRow 
      android:id="@+id/tableRow0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/introductionTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/heightPromptTextView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      > 

      <EditText 
       android:id="@+id/feetPortionOfHeightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       /> 

      <TextView 
       android:id="@+id/feetPortionOfHeightViewText" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 

       /> 

      <EditText 
       android:id="@+id/inchesPortionOfHeightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number" /> 

      <TextView 
       android:id="@+id/inchesPortionOfHeightViewText" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="0" 
       android:gravity="center_vertical" 
       android:padding="5sp" 
       android:paddingRight="5sp" 
       /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/weightPromptTextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" 
      android:paddingLeft="20sp" > 

      <EditText 
       android:id="@+id/weightEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       android:inputType="number" > 


      </EditText> 

      <TextView 
       android:id="@+id/weightTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" > 

      <Button 
       android:id="@+id/calculateButton" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5sp" > 

      <TextView 
       android:id="@+id/bmiResultTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/bmiResultCommentsTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       /> 

     </TableRow> 

    </TableLayout>