简体   繁体   English

RadioGroup导致Android App崩溃?

[英]RadioGroup causing Android App to crash?

I'm trying to build an app in Android Studio that calculates the local sales tax based on the purchase amount entered by the user. 我正在尝试在Android Studio中构建一个应用,该应用根据用户输入的购买金额来计算本地销售税。 However I think my radio groups are causing the app to crash because when I remove all references to them, the app opens just fine. 但是我认为我的广播组导致该应用崩溃,因为当我删除所有对它们的引用时,该应用就可以正常打开。

I checked the XML code and the IDE didn't give me any warnings other than using literal strings for naming conventions. 我检查了XML代码,除了使用文字字符串命名约定以外,IDE没有给我任何警告。

When I run the program, I get a message in the emulator saying "Unfortunately, week3app has stopped." 当我运行程序时,我在模拟器中收到一条消息,提示“不幸的是,week3app已停止。” No error that I can see shows up in the stack trace when I run the app unless it's one I'm unfamiliar with and I'm just not seeing it. 运行该应用程序时,我不会看到任何错误显示在堆栈跟踪中,除非它是我不熟悉的一个,而我只是没有看到它。 Here is my code: 这是我的代码:

XML Layout: 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>

Main Class: 主类:

    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");
                }
            }
        });    
    }
    }

And finally, the debug log: 最后,调试日志:

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'

I'm really stumped as to what's going on here. 我真的对这里发生的事情感到困惑。

You have a block of code that's trying to grab view references during the activity object instantion: 您有一段代码试图在活动对象实例化时获取视图引用:

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);

Instead you need to do all this only after setContentView is finished. 相反,只有在setContentView完成后才需要执行所有这些操作。 There are no views to find before then. 在此之前没有任何视图。

You are not instantiating editPurchAmt yet and trying to set text to editPurchAmt which is null right now . 您尚未实例化editPurchAmt ,并尝试将文本设置为editPurchAmt ,该文本现在为null please give reference to editPurchAmt from your view before use it. 请在使用前从您的视图中引用editPurchAmt

please move the following: 请移动以下内容:

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);

inside the onCreate method onCreate方法中

Replace your code of onCreate() to following, 将您的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");
                }
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM