我一直在寻找几个小时的解决方案。我正在尝试构建一个 BodyMassIndex 计算器。我已经完成了程序(我认为),但是每次我尝试运行该应用程序时,它都会在模拟器上崩溃。最重要的是,布局预览不再显示。我仍在尝试了解日志中所有错误代码的含义。我认为这是我的 layout.xml 的问题,但我不确定。任何建议或想法将不胜感激!
日志猫:
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>