我正在尝试在Android Studio中构建一个应用,根据用户输入的购买金额计算本地销售税。但是我认为我的广播组正在导致应用程序崩溃,因为当我删除对它们的所有引用时,应用程序打开就好了。
我检查了XML代码,除了使用文字字符串作为命名约定之外,IDE没有给我任何警告。
当我运行该程序时,我在模拟器中收到一条消息“不幸的是,week3app已经停止了”。当我运行应用程序时,我看到的任何错误都不会显示在堆栈跟踪中,除非它是我不熟悉的错误,我只是没有看到它。这是我的代码:
XML布局:
<?XML version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eliza.week3app.Main"
tools:showIn="@layout/activity_main"
android:background="#ebe8e8">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/zipEdit"
android:textSize="12sp"
android:hint="12345"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/findButton"
android:layout_alignStart="@+id/findButton"
android:layout_marginTop="33dp"
android:layout_toStartOf="@+id/purchAmtEdit"
android:layout_alignRight="@+id/findButton"
android:layout_alignEnd="@+id/findButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find Tax"
android:id="@+id/findButton"
android:layout_marginTop="53dp"
android:layout_below="@+id/AmountLbl"
android:layout_toRightOf="@+id/totalLbl"
android:layout_toEndOf="@+id/totalLbl" />
<RadioGroup
android:checkedButton="@+id/gpsRadio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radioGroup1"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/zipEdit"
android:layout_toStartOf="@+id/zipEdit"
android:layout_alignBottom="@+id/zipEdit">
<RadioButton
android:layout_width="124dp"
android:layout_height="wrap_content"
android:text="Use GPS"
android:id="@+id/gpsRadio"
android:textSize="12sp"
android:layout_marginLeft="20sp" />
<RadioButton
android:layout_width="124sp"
android:layout_height="wrap_content"
android:text="Use Zip"
android:id="@+id/zipRadio"
android:textSize="12sp"
android:layout_below="@+id/gpsRadio"
android:layout_marginLeft="20sp"/>
</RadioGroup>
<RadioGroup
android:checkedButton="@+id/generalRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup2"
android:layout_below="@+id/zipEdit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="124sp"
android:layout_height="wrap_content"
android:text="General Tax"
android:id="@+id/generalRadio"
android:textSize="12sp"
android:layout_marginTop="31sp"
android:layout_marginLeft="22sp"
android:layout_marginStart="22sp"/>
<RadioButton
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_marginLeft="22sp"
android:text="Grocery Tax"
android:id="@+id/groceryRadio"
android:textSize="12sp"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purchase Amount"
android:id="@+id/AmountLbl"
android:textColor="#030000"
android:layout_below="@+id/radioGroup2"
android:layout_toLeftOf="@+id/purchAmtEdit"
android:layout_toStartOf="@+id/purchAmtEdit"
android:layout_marginTop="29dp" />
<EditText
android:layout_width="130sp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/purchAmtEdit"
android:layout_above="@+id/findButton"
android:layout_toRightOf="@+id/radioGroup2"
android:layout_toEndOf="@+id/radioGroup2"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />
<TextView
android:layout_marginTop="85dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tax Amount: "
android:id="@+id/taxAmountLbl"
android:textColor="#030000"
android:layout_below="@+id/findButton"
android:layout_alignLeft="@+id/AmountLbl"
android:layout_alignStart="@+id/AmountLbl" />
<TextView
android:layout_marginTop="28dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Amount: "
android:id="@+id/totalLbl"
android:textColor="#030000"
android:layout_below="@+id/taxAmountLbl"
android:layout_alignLeft="@+id/taxAmountLbl"
android:layout_alignStart="@+id/taxAmountLbl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/taxAmtResultLbl"
android:textColor="#030000"
android:layout_above="@+id/totalLbl"
android:layout_toRightOf="@+id/totalLbl"
android:layout_toEndOf="@+id/totalLbl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/totalAmtResultLbl"
android:textColor="#030000"
android:layout_alignBottom="@+id/taxAmountLbl"
android:layout_toRightOf="@+id/taxAmtResultLbl"
android:layout_toEndOf="@+id/taxAmtResultLbl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location: "
android:id="@+id/locationLbl"
android:textColor="#030000"
android:layout_marginBottom="27dp"
android:layout_above="@+id/taxAmtResultLbl"
android:layout_alignLeft="@+id/taxAmountLbl"
android:layout_alignStart="@+id/taxAmountLbl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/locResultsLbl"
android:textColor="#030000"
android:layout_alignBottom="@+id/locationLbl"
android:layout_toRightOf="@+id/locationLbl"
android:layout_toEndOf="@+id/locationLbl"/>
</RelativeLayout>
主类:
package com.example.eliza.week3app;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Main extends Activity {
public RadioButton gpsRadio, zipRadio, genTaxRadio, grocTaxRadio;
public EditText editPurchAmt;
public String location;
public int zip;
public double localTax, totalTax, purchAmt, totalAmt;
RadioGroup locationGroup = (RadioGroup) findViewById(R.id.radioGroup1);
RadioGroup taxGroup = (RadioGroup) findViewById (R.id.radioGroup2);
EditText editZip = (EditText) findViewById(R.id.zipEdit);
EditText getPurchAmt = (EditText) findViewById(R.id.purchAmtEdit);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checked) {
if (checked == R.id.gpsRadio) {
editZip.setEnabled(false);
} else if (checked == R.id.zipRadio) {
editZip.setEnabled(true);
}
}
});
taxGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checked) {
if (checked == R.id.generalRadio) {
editPurchAmt.setText("100.00");
} else if (checked == R.id.zipRadio) {
editPurchAmt.setText("10.00");
}
}
});
}
}
最后,调试日志:
Target device: MyNexus [emulator-5554]
No apk changes detected since last installation, skipping installation of C:\Users\eliza\AndroidStudioProjects\Week3App\app\build\outputs\apk\app-debug.apk
Force stopping package: com.example.eliza.week3app
DEVICE SHELL COMMAND: am force-stop com.example.eliza.week3app
Launching application: com.example.eliza.week3app/com.example.eliza.week3app.Main.
DEVICE SHELL COMMAND: am start -D -n "com.example.eliza.week3app/com.example.eliza.week3app.Main" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
Client not ready yet.
WARNING: linker: libdvm. so has text relocations. This is wasting memory and is a security risk. Please fix.
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.eliza.week3app/.Main }
Connected to the target VM, address: 'localhost:8617', transport: 'socket'
Disconnected from the target VM, address: 'localhost:8617', transport: 'socket'
我真的很难过这里发生了什么。
答案 0 :(得分:1)
您有一段代码试图在活动对象实例期间抓取视图引用:
RadioGroup locationGroup = (RadioGroup) findViewById(R.id.radioGroup1);
RadioGroup taxGroup = (RadioGroup) findViewById (R.id.radioGroup2);
EditText editZip = (EditText) findViewById(R.id.zipEdit);
EditText getPurchAmt = (EditText) findViewById(R.id.purchAmtEdit);
相反,只有在setContentView完成后才需要完成所有这些操作。在此之前没有找到的意见。
答案 1 :(得分:0)
您尚未实例化editPurchAmt
并尝试将文本设置为editPurchAmt
,现在为null
。在使用之前,请先从您的视图中提及editPurchAmt
。
答案 2 :(得分:0)
请移动以下内容:
RadioGroup locationGroup = (RadioGroup) findViewById(R.id.radioGroup1);
RadioGroup taxGroup = (RadioGroup) findViewById (R.id.radioGroup2);
EditText editZip = (EditText) findViewById(R.id.zipEdit);
EditText getPurchAmt = (EditText) findViewById(R.id.purchAmtEdit);
在onCreate
方法
答案 3 :(得分:0)
将onCreate()的代码替换为以下内容,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup locationGroup = (RadioGroup) findViewById(R.id.radioGroup1);
RadioGroup taxGroup = (RadioGroup) findViewById (R.id.radioGroup2);
EditText editZip = (EditText) findViewById(R.id.zipEdit);
EditText getPurchAmt = (EditText) findViewById(R.id.purchAmtEdit);
locationGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checked) {
if (checked == R.id.gpsRadio) {
editZip.setEnabled(false);
} else if (checked == R.id.zipRadio) {
editZip.setEnabled(true);
}
}
});
taxGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checked) {
if (checked == R.id.generalRadio) {
editPurchAmt.setText("100.00");
} else if (checked == R.id.zipRadio) {
editPurchAmt.setText("10.00");
}
}
});
}