0

我的 Android 应用程序运行良好,直到我在 XML 布局中使用滚动视图和子容器进行更改,线性布局下有许多控件。此特定布局不再运行,并导致应用程序停止工作。此外,IDE 有时会说标记不是 Android 资源中的有效元素。但是,当我更新 IDE 时,它得到了修复。但是现在,这种带有计算的特定布局不起作用。什么可能是这个问题的解决方案。我真的真的需要尽快完成这个应用程序。:| 只有这个布局不起作用......

我打算再次创建一个新项目,然后复制粘贴 XML 布局,这可以解决我的问题吗?除此之外,我之前此布局正常工作的所有备份文件都不再工作了。我很难对付这个。

我正在使用 INTELLIJ LEDA 社区版 IDE。任何帮助,将不胜感激!这是 XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" android:background="#ffffff">

<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


    <Button android:layout_width="30dp" android:layout_height="30dp"
            android:id="@+id/btn_home_monthly" android:background="@drawable/home" android:textColor="#ffffff"
            android:layout_gravity="right"/>

    <Button android:layout_width="30dp" android:layout_height="30dp"
            android:id="@+id/btn_warning_from_monthlycomputation"
            android:background="@drawable/warn" android:layout_gravity="right"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Input your Monthly Net Salary:"
            android:id="@+id/textView" android:layout_gravity="center"
            android:textColor="@android:color/black" android:textStyle="bold"/>
    <EditText
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:id="@+id/et_net_monthly" android:layout_gravity="center" android:inputType="numberDecimal|numberSigned">
        <requestFocus/>
    </EditText>
    <Button android:layout_width="150dp" android:layout_height="40dp" android:text="Compute"
            android:id="@+id/btn_compute_from_monthly" android:background="@drawable/black_btn" android:textColor="#ffffff"
            android:layout_gravity="center"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:text="Tax Due:" android:id="@+id/textView1"
              android:layout_gravity="center" android:textColor="@android:color/black" android:textStyle="bold"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txt_taxdue" android:textColor="#ff0006" android:layout_gravity="center"
            android:textSize="20dp"
            android:textStyle="bold"/>

    <Button android:layout_width="150dp" android:layout_height="40dp" android:text="Show Breakdown"
            android:id="@+id/btn_showbreakdown" android:background="@drawable/black_btn"
            android:textColor="#ffffff" android:layout_gravity="center" android:visibility="invisible"/>


    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txt_1" android:layout_gravity="left|center_vertical" android:visibility="invisible"
            android:textColor="@android:color/black"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_2"
              android:layout_gravity="left|center_vertical" android:visibility="invisible"
              android:textColor="@android:color/black"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_3"
              android:layout_gravity="left|center_vertical" android:visibility="invisible"
              android:textColor="@android:color/black"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_4"
              android:layout_gravity="left|center_vertical" android:visibility="invisible"
              android:textColor="@android:color/black"/>


    <Button android:layout_width="150dp" android:layout_height="40dp" android:text="Save As Text File"
            android:id="@+id/btn_save" android:background="@drawable/black_btn" android:textColor="#ffffff"
            android:layout_gravity="center" android:visibility="invisible"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_filename"
              android:layout_gravity="left|center_vertical" android:visibility="invisible"
              android:textColor="@android:color/black"
              android:text="Name for File:"/>
    <EditText
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:id="@+id/et_txtname" android:layout_gravity="center" android:inputType="textFilter"
            android:visibility="invisible">
        <requestFocus/>
    </EditText>

    <Button android:layout_width="150dp" android:layout_height="40dp"
            android:id="@+id/btn_confirm" android:background="@drawable/save"
            android:layout_gravity="center" android:visibility="invisible"/>
</LinearLayout>
</ScrollView>

这是它的类:

 public class MonthlyComputation extends MyActivity
 {
 private String civil_status;
String ns, td, r, e, s, to, wr, n, txtname, stat;
String datetime = DateFormat.getDateTimeInstance().format(new Date());
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monthlycomputation);

    Bundle extras = getIntent().getExtras();

    if(extras != null)
    {
         civil_status = extras.getString("user_status");
    }

    Button btn_compute = (Button) findViewById(R.id.btn_compute_from_monthly);
    Button btn_warning = (Button) findViewById(R.id.btn_warning_from_monthlycomputation);
    final Button btn_show = (Button) findViewById(R.id.btn_showbreakdown);
    final Button btn_save = (Button) findViewById(R.id.btn_save);
    final Button btn_confirm = (Button) findViewById(R.id.btn_confirm);

    final EditText et_txtfile = (EditText) findViewById(R.id.et_txtname);
    final TextView txt_name = (EditText) findViewById(R.id.txt_filename);

    final Button btn_home = (Button) findViewById(R.id.btn_home_monthly);

    btn_home.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(getApplicationContext(), MyActivity.class);
            startActivity(i);
        }
    });
    /* SAVING TEXT FILE */

    btn_confirm.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            txtname = et_txtfile.getText().toString();

            /* Getting the status */

            if ("SME".equals(civil_status))
            {
                stat = "Single / Married with 0 Dependent";
            }

            else if ("SM1".equals(civil_status))
            {
                stat = "Single / Married with 1 Dependent";
            }

            else if ("SM2".equals(civil_status))
            {
                stat = "Single / Married with 2 Dependents";
            }

            else if ("SM3".equals(civil_status))
            {
                stat = "Single / Married with 3 Dependents";
            }

            else if ("SM4".equals(civil_status))
            {
                stat = "Single / Married with 4 Dependents";
            }
            try
            {
                File f1=new File("/sdcard/download/" + txtname + ".txt");
                BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));
                out.write("\n\nCreated On: " + datetime + " | Monthly Computation | " + stat + "\n\n"
                + "Net Salary: " + n + "\nTax Due: " + td + "\n\nBreakdown:\n\n"
                + "> Net Salary (" + n + ") - Lower Limit (" + s + ") = " + to +
                "\n" + "> " + to + " * Tax Rate (" + r + ") = " + wr + "\n"
                + "> Exemption (" + e + ") + " + wr + " = " + td + "\n"
                + "> Total Tax Due:  " + td + "\n");
                out.close();

                Toast toast = Toast.makeText(getApplicationContext(), "Your file " + txtname + " has been saved!", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
                toast.show();

                Intent i = new Intent(getApplicationContext(), OpenCalculator.class);
                startActivity(i);

            } catch (IOException ioe)
            {
                Toast toast = Toast.makeText(getApplicationContext(), "Sorry, your device has no SD card inserted, ITax saves files only on SD cards OR you don't have the destination folder, \"download\"", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
                toast.show();
                ioe.printStackTrace()
            ;}


        }
    });


    btn_warning.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(getApplicationContext(), SalaryNoteMonthly.class);
            startActivity(i);
        }
    });


    btn_compute.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            final EditText net_salary = (EditText) findViewById(R.id.et_net_monthly);
            final TextView tax_due = (TextView) findViewById(R.id.txt_taxdue);

            double netSalary, taxDue, rate = 0, exemption = 0, subtrahend = 0, withRate, total;


            ns = net_salary.getText().toString();

            /* checks if textbox is blank, then make it to 0 */

            if(ns.equals(""))
            {
                netSalary = 0;
                Toast toast = Toast.makeText(getApplicationContext(), "Net Salary is blank.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
                toast.show();
            }

            else
            {
                netSalary = Double.parseDouble(ns);
            }

            /* Converting Net Salary to String with 2 decimal places */

            n = String.format("%.2f", netSalary);

            /* Single or Married with no dependent */

                    if ("SME".equals(civil_status)) {
                        if (netSalary >= 4167 && netSalary < 5000) {
                            rate = 0.05;
                            exemption = 0.00;
                            subtrahend = 4167;
                        } else if ((netSalary >= 5000) && (netSalary < 6667)) {
                            rate = 0.1;
                            exemption = 41.67;
                            subtrahend = 5000;
                        } else if ((netSalary >= 6667) && (netSalary < 10000)) {
                            rate = 0.15;
                            exemption = 208.33;
                            subtrahend = 6667;
                        } else if ((netSalary >= 10000) && (netSalary < 15833)) {
                            rate = 0.20;
                            exemption = 708.33;
                            subtrahend = 10000;
                        } else if ((netSalary >= 15833) && (netSalary < 25000)) {
                            rate = 0.25;
                            exemption = 1875.00;
                            subtrahend = 15833;
                        } else if ((netSalary >= 25000) && (netSalary < 45833)) {
                            rate = 0.30;
                            exemption = 4166.67;
                            subtrahend = 25000;
                        } else if (netSalary >= 45833) {
                            rate = 0.32;
                            exemption = 10416.67;
                            subtrahend = 45833;
                        }
                    }

            /* Single or Married with 1 Dependent */

                    if ("SM1".equals(civil_status)) {
                        if ((netSalary >= 6250) && (netSalary < 7083)) {
                            rate = 0.05;
                            exemption = 0.00;
                            subtrahend = 6250;
                        } else if ((netSalary >= 7083) && (netSalary < 8750)) {
                            rate = 0.1;
                            exemption = 41.67;
                            subtrahend = 7083;
                        } else if ((netSalary >= 8750) && (netSalary < 12083)) {
                            rate = 0.15;
                            exemption = 208.33;
                            subtrahend = 8750;
                        } else if ((netSalary >= 12083) && (netSalary < 17917)) {
                            rate = 0.20;
                            exemption = 708.33;
                            subtrahend = 12083;
                        } else if ((netSalary >= 17917) && (netSalary < 27083)) {
                            rate = 0.25;
                            exemption = 1875.00;
                            subtrahend = 17917;
                        } else if ((netSalary >= 27083) && (netSalary < 47917)) {
                            rate = 0.30;
                            exemption = 4166.67;
                            subtrahend = 27083;
                        } else if (netSalary >= 47917) {
                            rate = 0.32;
                            exemption = 10416.67;
                            subtrahend = 47917;
                        }
                    }

             /* Single or Married with 2 Dependents */

                    if ("SM2".equals(civil_status)) {
                        if ((netSalary >= 8333) && (netSalary < 9167)) {
                            rate = 0.05;
                            exemption = 0.00;
                            subtrahend = 8333;
                        } else if ((netSalary >= 9167) && (netSalary < 10833)) {
                            rate = 0.1;
                            exemption = 41.67;
                            subtrahend = 9167;
                        } else if ((netSalary >= 10833) && (netSalary < 14167)) {
                            rate = 0.15;
                            exemption = 208.33;
                            subtrahend = 10833;
                        } else if ((netSalary >= 14167) && (netSalary < 20000)) {
                            rate = 0.20;
                            exemption = 708.33;
                            subtrahend = 14167;
                        } else if ((netSalary > 20000) && (netSalary < 29167)) {
                            rate = 0.25;
                            exemption = 1875.00;
                            subtrahend = 20000;
                        } else if ((netSalary >= 29167) && (netSalary < 50000)) {
                            rate = 0.30;
                            exemption = 4166.67;
                            subtrahend = 29167;
                        } else if (netSalary >= 50000) {
                            rate = 0.32;
                            exemption = 10416.67;
                            subtrahend = 50000;
                        }
                    }

        /* Single or Married with 3 Dependents */

                    if ("SM3".equals(civil_status)) {
                        if ((netSalary >= 10417) && (netSalary < 11250)) {
                            rate = 0.05;
                            exemption = 0.00;
                            subtrahend = 10417;
                        } else if ((netSalary >= 11250) && (netSalary < 12917)) {
                            rate = 0.1;
                            exemption = 41.67;
                            subtrahend = 11250;
                        } else if ((netSalary >= 12917) && (netSalary < 16250)) {
                            rate = 0.15;
                            exemption = 208.33;
                            subtrahend = 12917;
                        } else if ((netSalary >= 16250) && (netSalary < 22083)) {
                            rate = 0.20;
                            exemption = 708.33;
                            subtrahend = 16250;
                        } else if ((netSalary >= 22083) && (netSalary < 31250)) {
                            rate = 0.25;
                            exemption = 1875.00;
                            subtrahend = 22083;
                        } else if ((netSalary >= 31250) && (netSalary < 52083)) {
                            rate = 0.30;
                            exemption = 4166.67;
                            subtrahend = 31250;
                        } else if (netSalary >= 52083) {
                            rate = 0.32;
                            exemption = 10416.67;
                            subtrahend = 52083;
                        }

                    }

             /* Single or Married with 4 Dependents */

                    if ("SM4".equals(civil_status)) {
                        if ((netSalary >= 12500) && (netSalary < 13333)) {
                            rate = 0.05;
                            exemption = 0.00;
                            subtrahend = 12500;
                        } else if ((netSalary >= 13333) && (netSalary < 15000)) {
                            rate = 0.1;
                            exemption = 41.67;
                            subtrahend = 13333;
                        } else if ((netSalary >= 15000) && (netSalary < 18333)) {
                            rate = 0.15;
                            exemption = 208.33;
                            subtrahend = 15000;
                        } else if ((netSalary >= 18333) && (netSalary < 24167)) {
                            rate = 0.20;
                            exemption = 708.33;
                            subtrahend = 18383;
                        } else if ((netSalary >= 24167) && (netSalary < 33333)) {
                            rate = 0.25;
                            exemption = 1875.00;
                            subtrahend = 24167;
                        } else if ((netSalary >= 33333) && (netSalary < 54167)) {
                            rate = 0.30;
                            exemption = 4166.67;
                            subtrahend = 33333;
                        } else if ((netSalary >= 54167)) {
                            rate = 0.32;
                            exemption = 10416.67;
                            subtrahend = 54167;
                        }
                    }

                    total = netSalary - subtrahend;
                    withRate = total * rate;
                    taxDue = withRate + exemption;

                    td = String.format("%.2f",taxDue);
                    e = String.format("%.2f", exemption);
                    s = String.format("%.2f", subtrahend);
                    r = String.format("%.2f", rate);
                    to = String.format("%.2f", total);
                    wr = String.format("%.2f", withRate);

            /*Place the value in text view*/

                    tax_due.setText("Php " + td);

            /*BREAK DOWN*/

            final TextView tax_1 = (TextView) findViewById(R.id.txt_1);
            final TextView tax_2 = (TextView) findViewById(R.id.txt_2);
            final TextView tax_3 = (TextView) findViewById(R.id.txt_3);
            final TextView tax_4 = (TextView) findViewById(R.id.txt_4);

            tax_1.setText("> Net Salary (" + n + ") - Lower Limit (" + s + ") = " + to );
            tax_2.setText("> " + to + " * Tax Rate (" + r + ") = " + wr);
            tax_3.setText("> Exemption (" + e + ") + " + wr + " = " + td);
            tax_4.setText("> Total Tax Due:  " + td);

            btn_show.setVisibility(1);
            btn_save.setVisibility(1);



        }
    });

    btn_save.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            btn_confirm.setVisibility(1);
            et_txtfile.setVisibility(1);
            txt_name.setVisibility(1);
        }
    });

    btn_show.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            final TextView tax_1 = (TextView) findViewById(R.id.txt_1);
            final TextView tax_2 = (TextView) findViewById(R.id.txt_2);
            final TextView tax_3 = (TextView) findViewById(R.id.txt_3);
            final TextView tax_4 = (TextView) findViewById(R.id.txt_4);

            tax_1.setVisibility(1);
            tax_2.setVisibility(1);
            tax_3.setVisibility(1);
            tax_4.setVisibility(1);
        }
    });
}

@Override
public void onBackPressed()
{
    Intent i = new Intent(getApplicationContext(), OpenChoices.class);
    startActivity(i);
}
 }

这是日志猫:

~ 频道无法恢复,将被处理掉!11-15 21:37:17.735:错误/InputDispatcher(157):收到未知输入通道的虚假接收回调。fd=183, events=0x9 11-15 21:37:57.894: ERROR/Trace(1113): error opening trace file: No such file or directory (2) 11-15 21:38:27.525: ERROR/Trace(1154) ): 打开跟踪文件时出错: 没有这样的文件或目录 (2) 11-15 21:38:32.645: ERROR/ThrottleService(157): onPollAlarm 期间出现问题: java.lang.IllegalStateException: 解析统计问题: java.io.FileNotFoundException :/proc/net/xt_qtaguid/iface_stat_all:打开失败:ENOENT(没有这样的文件或目录)11-15 21:40:06.105:错误/AndroidRuntime(1154):致命异常:主要 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.ITax/com.example.ITax.MonthlyComputation}:java.lang.ClassCastException:553) 在 dalvik.system.NativeStart.main(Native Method) 引起:java.lang.ClassCastException: android.widget.TextView 不能在 com.example.ITax.MonthlyComputation.onCreate(MonthlyComputation. java:47) 在 android.app.Activity.performCreate(Activity.java:5008) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 。 .. 11 更多 11-15 21:40:08.395: ERROR/InputDispatcher(157): channel '413830d8 com.example.ITax/com.example.ITax.OpenChoices (server)' ~ Channel 已不可恢复地损坏,将被处理!11-15 21:40:08.475:ERROR/InputDispatcher(157):收到未知输入通道的虚假接收回调。fd=197,事件=0x9 11-15 21:43:10.085:

4

2 回答 2

1

The "Name for file" control is a TextView, and you're casting it to EditText, which fails because it's the wrong type. You need to cast to TextView instead in the following line:

final TextView txt_name = (EditText) findViewById(R.id.txt_filename);

于 2013-01-15T19:00:43.813 回答
1

每当您的应用程序崩溃或出现问题时,请尝试读取 logcat,它可能会为您提供确切的错误点。

这里说Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at

这是因为您试图将 EditText 转换为 TextView。它应该是这样的:

final EditText txt_name = (EditText) findViewById(R.id.txt_filename);
于 2013-01-15T19:16:35.157 回答