不幸的是,“app_name”已停止工作

时间:2015-04-18 20:19:11

标签: java android xml

我正在尝试开发一个非常简单的应用程序,用于计算住房抵押贷款的总利息。有2个屏幕。第一个用户输入他们的每月付款,他们的贷款年数和初始贷款金额。第一个屏幕加载就好了。但是当点击按钮时,我会得到一个好老的"不幸的是#34; app"已停止工作。想知道是否有人可以解释为什么。这是该屏幕的java代码:

package com.example.mortgage;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

package com.example.mortgage;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    int enterPayment;
    int enterYears;
    int enterPrincipal;
    int totalInterest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText payment=(EditText)findViewById(R.id.txtPayment);
        final EditText years=(EditText)findViewById(R.id.txtYear);
        final EditText principal=(EditText)findViewById(R.id.txtPrincipal);

        Button btCompute = (Button)findViewById(R.id.btnCompute);
        final SharedPreferences                 sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
        btCompute.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                enterPayment =    Integer.parseInt(payment.getText().toString());
                enterYears = Integer.parseInt(years.getText().toString());
                enterPrincipal = Integer.parseInt(principal.getText().toString());

                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putInt("key1", enterPayment);
                editor.putInt("key2", enterYears);
                editor.putInt("key3", enterPrincipal);
                editor.commit();
                startActivity(new Intent(MainActivity.this, Status.class));

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is        present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

该屏幕的XML文件:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mortgage.MainActivity" >

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="@string/txtTitle"
    android:textSize="30sp" />

<EditText
    android:id="@+id/txtPayment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:ems="10"
    android:inputType="number" 
    android:hint="@string/hint1" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtYear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtPayment"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp"
    android:ems="10"
    android:inputType="number" 
    android:hint="@string/hint2" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtPrincipal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtYear"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:ems="10"
    android:inputType="number" 
    android:hint="@string/hint3" >

    <requestFocus />
</EditText>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="177dp"
    android:src="@drawable/mortgage"
    android:contentDescription="@string/imgMortgage" />

<Button
    android:id="@+id/btnCompute"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="26dp"
    android:text="@string/btnCompute" />

 </RelativeLayout>

下一个屏幕显示计算的兴趣。

  package com.example.mortgage;



 public class Status extends Activity {
int months;
int totalInterest;
@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.status);
    TextView result = (TextView)findViewById(R.id.txtTotal);
    ImageView image = (ImageView)findViewById(R.id.imgYearLoan);

    SharedPreferences sharedPref =   PreferenceManager.getDefaultSharedPreferences(this);
    int intPayment = sharedPref .getInt("key1", 0);
    int intYears = sharedPref.getInt("key2", 0);
    int intLoan = sharedPref.getInt("key3", 0);

    if(intYears == 10){
        months = 120;
        totalInterest = (intPayment * months) - intLoan;

    }else if(intYears == 20){
        months = 240;
        totalInterest = (intPayment * months) - intLoan;
    }else {
        months = 360;
        totalInterest = (intPayment * months) - intLoan;
    }

    result.setText(totalInterest);


    image.setImageResource(R.drawable.ten);

  }
 }

该屏幕的XML:

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="@string/txtTitle" />

<TextView
    android:id="@+id/txtTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:ems="10"
    android:hint="@string/hint4" />

<ImageView
    android:id="@+id/imgYearLoan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="95dp"
    android:contentDescription="@string/imgYearLoan" />

</RelativeLayout>

错误日志只是说&#34;未处理的事件循环eception&#34;

1 个答案:

答案 0 :(得分:2)

您使用整数而不是字符串调用setText()

result.setText(totalInterest);

因此,Android正在尝试使用指定的(无效)标识符加载字符串资源。相反,做这样的事情:

result.setText("" + totalInterest);