gpt4 book ai didi

java - 不幸的是 "app_name"已停止工作

转载 作者:行者123 更新时间:2023-12-02 04:45:18 25 4
gpt4 key购买 nike

我正在尝试开发一个非常简单的应用程序,用于计算住房抵押贷款支付的总利息。有2个屏幕。第一个要求用户输入每月还款额、贷款年限和初始贷款金额。第一个屏幕加载得很好。但是,当单击该按钮时,我得到了“不幸的是,“应用程序”已停止工作”的提示。想知道是否有人可以解释为什么。以下是该屏幕的 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>

错误日志只显示“未处理的事件循环接收”

最佳答案

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

result.setText(totalInterest);

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

result.setText("" + totalInterest);

关于java - 不幸的是 "app_name"已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29722359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com