【发布时间】:2019-02-06 14:23:01
【问题描述】:
我的应用程序出现问题。按下任何按钮都会崩溃。
我已经使用了字符串对象的浮点转换。
我的 MainActivity.java 文件:
package com.example.admin.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView tv1,tv2;
EditText et1;
Button btn1,btn2,btn3,btn4;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=findViewById(R.id.tv1);
tv2=findViewById(R.id.tv2);
et1=findViewById(R.id.et1);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn4=findViewById(R.id.btn4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
tv2.findViewById(R.id.tv2);
String value=et1.getText().toString();
int newvalue=Integer.parseInt(value);
int thirdvalue=0;
switch (v.getId()){
case R.id.btn1:
thirdvalue = (int) (newvalue*37.98f);
tv2.setText(thirdvalue);
break;
case R.id.btn2:
thirdvalue = (int) (newvalue*45.47f);
tv2.setText(thirdvalue);
break;
case R.id.btn3:
thirdvalue = (int) (newvalue*50.52f);
tv2.setText(thirdvalue);
break;
case R.id.btn4:
thirdvalue = (int) (newvalue*63.16f);
tv2.setText(thirdvalue);
break;
}
}
}
我想在按下任何按钮时获得结果,并根据按下的按钮给出结果。
它在我的 logcat 中给出了这个错误:
E/AndroidRuntime: 致命异常: main 进程:com.example.admin.myapplication,PID:14892 android.content.res.Resources$NotFoundException:字符串资源 ID #0x3b5 在 android.content.res.Resources.getText(Resources.java:328) 在 android.content.res.MiuiResources.getText(MiuiResources.java:125) 在 android.widget.TextView.setText(TextView.java:4432) 在 com.example.admin.myapplication.MainActivity.onClick(MainActivity.java:45) 在 android.view.View.performClick(View.java:5215) 在 android.view.View$PerformClick.run(View.java:21196) 在 android.os.Handler.handleCallback(Handler.java:742) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:157) 在 android.app.ActivityThread.main(ActivityThread.java:5603) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) I/Process:发送信号。 PID:14892 SIG:9 应用程序终止。
【问题讨论】:
-
将
tv2.setText(thirdvalue);之类的所有语句替换为tv2.setText("" + thirdvalue); -
谢谢@forpas。它有效。