我正在开发我的第一个Android应用程序。它几乎是一个基本的小费计算器。出于某种原因,我无法让Eclipse认识到已插入电话,因此我无法通过Eclipse对其进行调试。 (我已经按照这里的所有步骤无效:http://developer.android.com/tools/extras/oem-usb.html)我已经在模拟器上成功安装了它,但每当我打开它时,它立即崩溃。我已尝试使用两种不同的XML布局,我将在下面发布,但我对我的应用可能崩溃的原因感兴趣。它很简单;只有一项活动,但我找不到错误。我知道它看起来太过分了,但任何帮助都非常感激。谢谢大家。
应用代码:
package com.teamjava.theultimatetipconverter;
import android.app.Activity;
import android.os.Bundle;
//import android.view.Menu;
//import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
//import android.os.Build;
//import android.app.ActionBar;
public class MainActivity extends Activity {
//create elements
private Button mCalculate;
private RadioButton mStupendous;
private RadioButton mAboveAverage;
private RadioButton mStandard;
private RadioButton mSubpar;
private EditText mCost;
private EditText mTip;
private EditText mTotal;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCalculate = (Button)findViewById(R.id.calculate);
mStupendous = (RadioButton)findViewById(R.id.radio0);
mAboveAverage = (RadioButton)findViewById(R.id.radio2);
mStandard = (RadioButton)findViewById(R.id.radio1);
mSubpar = (RadioButton)findViewById(R.id.radio4);
mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
mRadioGroup.addView(mStupendous);
mRadioGroup.addView(mAboveAverage);
mRadioGroup.addView(mStandard);
mRadioGroup.addView(mSubpar);
mRadioGroup.check(R.id.radio1);
mCost = (EditText)findViewById(R.id.EditText01);
mTip = (EditText)findViewById(R.id.editText5);
mTotal = (EditText)findViewById(R.id.editText6);
mCalculate.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mStupendous.isSelected()){
double bill = Double.valueOf(mCost.toString());
String billString = String.valueOf(bill * .25);
mTip.setText("$" + billString);
mTotal.setText("$" + String.valueOf((bill + Double.parseDouble(billString))));
}
else if(mAboveAverage.isSelected()){
double bill = Double.valueOf(mCost.toString());
String billString = String.valueOf(bill * .2);
mTip.setText("$" + billString);
mTotal.setText("$" + String.valueOf((bill + Double.parseDouble(billString))));
}
else if(mStandard.isSelected()){
double bill = Double.valueOf(mCost.toString());
String billString = String.valueOf(bill * .15);
mTip.setText("$" + billString);
mTotal.setText("$" + String.valueOf((bill + Double.parseDouble(billString))));
}
else if(mSubpar.isSelected()){
double bill = Double.valueOf(mCost.toString());
String billString = String.valueOf(bill * .1);
mTip.setText("$" + billString);
mTotal.setText("$" + String.valueOf((bill + Double.parseDouble(billString))));
}
}
}));
}
}
Android清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teamjava.theultimatetipconverter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.teamjava.theultimatetipconverter.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML Layout one:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:orientation="horizontal" >
<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_gravity="left"
android:layout_row="6"
android:layout_rowSpan="2"
android:gravity="center_vertical|center_horizontal|left"
android:text="@string/calculate" />
<TextView
android:id="@+id/textView4"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="8"
android:text="@string/suggestedTip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText5"
android:layout_width="130dp"
android:layout_column="2"
android:layout_gravity="left"
android:layout_row="8"
android:layout_rowSpan="2"
android:ems="10"
android:inputType="none|numberDecimal" />
<TextView
android:id="@+id/textView5"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="10"
android:text="@string/totalBill"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText6"
android:layout_width="130dp"
android:layout_column="2"
android:layout_gravity="left"
android:layout_row="10"
android:ems="10"
android:inputType="none|numberDecimal" />
<Space
android:layout_width="105dp"
android:layout_height="1dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="26dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="187dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="112dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="28dp"
android:layout_column="0"
android:layout_row="1" />
<Space
android:layout_width="1dp"
android:layout_height="38dp"
android:layout_column="0"
android:layout_row="2" />
<Space
android:layout_width="1dp"
android:layout_height="63dp"
android:layout_column="0"
android:layout_row="3" />
<Space
android:layout_width="1dp"
android:layout_height="24dp"
android:layout_column="0"
android:layout_row="5" />
<Space
android:layout_width="1dp"
android:layout_height="32dp"
android:layout_column="0"
android:layout_row="6" />
<Space
android:layout_width="1dp"
android:layout_height="32dp"
android:layout_column="0"
android:layout_row="7" />
<Space
android:layout_width="1dp"
android:layout_height="27dp"
android:layout_column="0"
android:layout_row="8" />
<Space
android:layout_width="1dp"
android:layout_height="37dp"
android:layout_column="0"
android:layout_row="9" />
<Space
android:layout_width="1dp"
android:layout_height="62dp"
android:layout_column="0"
android:layout_row="10" />
<Space
android:layout_width="24dp"
android:layout_height="15dp"
android:layout_column="1"
android:layout_row="0" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="4" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/standard" />
</RadioGroup>
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:checked="true"
android:text="@string/stupendous" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/aboveAverage" />
<EditText
android:id="@+id/EditText01"
android:layout_width="126dp"
android:layout_column="2"
android:layout_gravity="left|bottom"
android:layout_row="0"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="left|top"
android:layout_row="4"
android:text="@string/subpar" />
<TextView
android:id="@+id/textView1"
android:layout_width="136dp"
android:layout_height="37dp"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="0"
android:text="@string/tipConverter"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_height="39dp"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="0"
android:text="@string/costOfMeal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="120dp"
android:layout_column="0"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/serviceQuality"
android:textAppearance="?android:attr/textAppearanceMedium" />
</GridLayout>
XML布局二:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:text="Cost of Meal"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="2dp"
android:layout_y="7dp" />
<EditText
android:id="@+id/EditText01"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff000000"
android:numeric="decimal"
android:layout_x="179dp"
android:layout_y="7dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="27dp"
android:text="How was your service?"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="4dp"
android:layout_y="52dp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="198dp"
android:layout_height="191dp"
android:layout_x="2dp"
android:layout_y="82dp">
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stupendous"
android:textColor="#ff000000" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Above Average"
android:textColor="#ff000000" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Standard"
android:textColor="#ff000000" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subpar"
android:textColor="#ff000000" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suggested Tip"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="2dp"
android:layout_y="353dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Bill"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="2dp"
android:layout_y="396dp" />
<EditText
android:id="@+id/editText5"
android:layout_width="107dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff000000"
android:numeric="decimal"
android:layout_x="191dp"
android:layout_y="345dp" />
<EditText
android:id="@+id/editText6"
android:layout_width="107dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff000000"
android:password="true"
android:numeric="decimal"
android:layout_x="190dp"
android:layout_y="389dp" />
<Button
android:id="@+id/widget46"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:textSize="18sp"
android:layout_x="204dp"
android:layout_y="284dp" />
</AbsoluteLayout>
编辑:以下是LogCat的错误日志:
05-07 23:09:09.732: D/dalvikvm(2112): GC_FOR_ALLOC freed 64K, 4% free 3523K/3664K, paused 29ms, total 34ms
05-07 23:09:09.902: D/dalvikvm(2112): GC_FOR_ALLOC freed 12K, 3% free 3950K/4064K, paused 27ms, total 30ms
05-07 23:09:10.032: D/AndroidRuntime(2112): Shutting down VM
05-07 23:09:10.032: W/dalvikvm(2112): threadid=1: thread exiting with uncaught exception (group=0xb0d40b20)
05-07 23:09:10.042: E/AndroidRuntime(2112): FATAL EXCEPTION: main
05-07 23:09:10.042: E/AndroidRuntime(2112): Process: com.teamjava.theultimatetipconverter, PID: 2112
05-07 23:09:10.042: E/AndroidRuntime(2112): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamjava.theultimatetipconverter/com.teamjava.theultimatetipconverter.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.os.Looper.loop(Looper.java:136)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 23:09:10.042: E/AndroidRuntime(2112): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 23:09:10.042: E/AndroidRuntime(2112): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 23:09:10.042: E/AndroidRuntime(2112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 23:09:10.042: E/AndroidRuntime(2112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 23:09:10.042: E/AndroidRuntime(2112): at dalvik.system.NativeStart.main(Native Method)
05-07 23:09:10.042: E/AndroidRuntime(2112): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.view.ViewGroup.addView(ViewGroup.java:3415)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.widget.RadioGroup.addView(RadioGroup.java:141)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.view.ViewGroup.addView(ViewGroup.java:3360)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.view.ViewGroup.addView(ViewGroup.java:3336)
05-07 23:09:10.042: E/AndroidRuntime(2112): at com.teamjava.theultimatetipconverter.MainActivity.onCreate(MainActivity.java:39)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.Activity.performCreate(Activity.java:5231)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-07 23:09:10.042: E/AndroidRuntime(2112): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-07 23:09:10.042: E/AndroidRuntime(2112): ... 11 more
答案 0 :(得分:1)
你有一件事就是在这里你说mCalculate是一个Button。
private Button mCalculate;
并将布局的视图R.id.button1实例化为Button
mCalculate = (Button)findViewById(R.id.button1);
但你的布局说R.id.button是一个GridLayout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:orientation="horizontal" >
我认为你想要实例化Button视图R.id.calculate
mCalculate = (Button)findViewById(R.id.calculate);
同样,当执行失败时,请在DDMS视图中看到Logcat并发布错误日志。它有重要的信息可以解决问题。
我认为问题是你试图将radiobuttons添加到radiogroup,但是radiobuttons已经有了一个父,它在布局文件中定义。
尝试,而不是在布局中的第一个单选按钮后关闭Radiogroup标签,将所有单选按钮放在一起并在最后一个之后关闭标签。并从程序中删除所有mRadioGroup.addView(...)。
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="4" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/standard" />
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:checked="true"
android:text="@string/stupendous" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/aboveAverage" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="left|top"
android:layout_row="4"
android:text="@string/subpar" />
</RadioGroup>