这就是logcat所说的: 有什么问题?
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4147)
at android.view.View$PerformClick.run(View.java:17161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4147)
at android.view.View$PerformClick.run(View.java:17161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3
at android.content.res.Resources.getText(Resources.java:234)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:53)
at android.widget.TextView.setText(TextView.java:3650)
at com.example.una.basketcounter2.MainActivity.displaypoinB(MainActivity.java:50)
at com.example.una.basketcounter2.MainActivity.klik3poinB(MainActivity.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4147)
at android.view.View$PerformClick.run(View.java:17161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
这是我的java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int poinA = 0;
int poinB = 0;
public void displaypoinA (int poin){
TextView label = (TextView) findViewById(R.id.poinTimA);
label.setText(poin);
}
public void klik3poinA (View view){
poinA= poinA + 3;
displaypoinA (poinA);
}
public void klik2poinA (View view){
poinA= poinA + 2;
displaypoinA (poinA);
}
public void klik1poinA (View view){
poinA= poinA + 1;
displaypoinA (poinA);
}
public void displaypoinB (int poin){
TextView label = (TextView) findViewById(R.id.poinTimB);
label.setText(poin);
}
public void klik3poinB (View view){
poinB= poinB + 3;
displaypoinB (poinB);
}
public void klik2poinB (View view){
poinB= poinB + 2;
displaypoinB (poinB);
}
public void klik1poinB (View view){
poinB= poinB + 1;
displaypoinB (poinB);
}
}
答案 0 :(得分:0)
在进行setText()
时,只需在所有位置更改此行 label.setText(String.valueOf(poin));
因为您在setText()中传递了int值。
答案 1 :(得分:0)
试试这个
label.setText(""+poin);
希望这会奏效。让我知道它是否有效
答案 2 :(得分:0)
注意:请记住textview只接受字符串值,如果你想要show int,float,double等你需要添加" "使用textview中的值,如下所示。
label.setText("" + poin);
OR
label.setText(String.valueOf(poin));
使用以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int poinA = 0;
int poinB = 0;
public void displaypoinA(int poin) {
TextView label = (TextView) findViewById(R.id.poinTimA);
label.setText("" + poin);
}
public void klik3poinA(View view) {
poinA = poinA + 3;
displaypoinA(poinA);
}
public void klik2poinA(View view) {
poinA = poinA + 2;
displaypoinA(poinA);
}
public void klik1poinA(View view) {
poinA = poinA + 1;
displaypoinA(poinA);
}
public void displaypoinB(int poin) {
TextView label = (TextView) findViewById(R.id.poinTimB);
label.setText("" + poin);
}
public void klik3poinB(View view) {
poinB = poinB + 3;
displaypoinB(poinB);
}
public void klik2poinB(View view) {
poinB = poinB + 2;
displaypoinB(poinB);
}
public void klik1poinB(View view) {
poinB = poinB + 1;
displaypoinB(poinB);
}
}