我正在创建一个只包含三个按钮的简单Android应用程序:加上按钮,减去按钮和重置按钮。 但不幸的是,当我按下三个按钮之一时,我的应用程序崩溃了。
以下是我的主要活动代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener{
Button btnAdd, btnMinus, btnReset;
TextView tvCounter;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnAdd = (Button)findViewById(R.id.addButton);
btnMinus = (Button)findViewById(R.id.minusButton);
btnReset = (Button)findViewById(R.id.resetButton);
btnAdd.setOnClickListener(this);
btnMinus.setOnClickListener(this);
btnReset.setOnClickListener(this);
tvCounter = (TextView)findViewById(R.id.counterTextView);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.addButton:{
count++;
tvCounter.setText(count);
}
break;
case R.id.minusButton:{
count--;
tvCounter.setText(count);
}
break;
case R.id.resetButton:{
count = 0;
tvCounter.setText(count);
}
break;
default:
break;
}
}
}
这是我的布局文件:
<?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"
android:background="@drawable/tasbih"
android:orientation="vertical" >
<TextView
android:id="@+id/counterTextView"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:textSize="50sp"
android:textColor="#000000"
android:layout_marginTop="30dp"
android:text="0"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/addButton"
style="@style/buttonStyle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="+" />
<Button
android:id="@+id/resetButton"
style="@style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/addButton"
android:layout_toLeftOf="@+id/addButton"
android:text="Reset" />
<Button
android:id="@+id/minusButton"
style="@style/buttonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/addButton"
android:layout_toRightOf="@+id/addButton"
android:text="-" />
</RelativeLayout>
请建议我走向正确。我是初学者
Log-cat:
08-21 20:44:45.693: D/ActivityThread(20653): handleBindApplication:com.example.subhanallah
08-21 20:44:45.723: W/ApplicationPackageManager(20653): getCSCPackageItemText()
08-21 20:44:45.723: D/DisplayManager(20653): DisplayManager()
08-21 20:44:45.803: D/OpenGLRenderer(20653): Enabling debug mode 0
08-21 20:44:45.833: W/IInputConnectionWrapper(20653): showStatusIcon on inactive InputConnection
08-21 20:44:45.843: I/Timeline(20653): Timeline: Activity_idle id: android.os.BinderProxy@41ea3d50 time:18178662
08-21 20:44:46.764: I/Timeline(20653): Timeline: Activity_launch_request id:com.example.subhanallah time:18179580
08-21 20:44:46.784: W/ApplicationPackageManager(20653): getCSCPackageItemText()
08-21 20:44:46.944: I/Timeline(20653): Timeline: Activity_idle id: android.os.BinderProxy@41f688a0 time:18179769
08-21 20:45:06.213: I/Timeline(20653): Timeline: Activity_idle id: android.os.BinderProxy@41f688a0 time:18199034
08-21 20:45:07.885: W/ResourceType(20653): No package identifier when getting value for resource number 0x00000001
08-21 20:45:07.885: D/AndroidRuntime(20653): Shutting down VM
08-21 20:45:07.885: W/dalvikvm(20653): threadid=1: thread exiting with uncaught exception (group=0x41702bc0)
08-21 20:45:07.895: E/AndroidRuntime(20653): FATAL EXCEPTION: main
08-21 20:45:07.895: E/AndroidRuntime(20653): Process: com.example.subhanallah, PID: 20653
08-21 20:45:07.895: E/AndroidRuntime(20653): android.content.res.Resources$NotFoundException: String resource ID #0x1
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.content.res.Resources.getText(Resources.java:1404)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.widget.TextView.setText(TextView.java:4262)
08-21 20:45:07.895: E/AndroidRuntime(20653): at com.example.subhanallah.Main.onClick(Main.java:39)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.view.View.performClick(View.java:4496)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.view.View$PerformClick.run(View.java:18603)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.os.Handler.handleCallback(Handler.java:733)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.os.Handler.dispatchMessage(Handler.java:95)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.os.Looper.loop(Looper.java:136)
08-21 20:45:07.895: E/AndroidRuntime(20653): at android.app.ActivityThread.main(ActivityThread.java:5433)
08-21 20:45:07.895: E/AndroidRuntime(20653): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 20:45:07.895: E/AndroidRuntime(20653): at java.lang.reflect.Method.invoke(Method.java:515)
08-21 20:45:07.895: E/AndroidRuntime(20653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
08-21 20:45:07.895: E/AndroidRuntime(20653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
08-21 20:45:07.895: E/AndroidRuntime(20653): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您是否有意将tvCounter
的TEXT设置为整数count
?您应该将文本设置为字符串值tvCounter.setText(String.valueOf(count));
,除非您打算通过字符串资源ID设置文本。
setText
函数可以接受引用字符串资源的int值(请参阅http://developer.android.com/reference/android/widget/TextView.html#setText(int))。您提供的给定int
(count
)
另外还有关于switch-case语句的另一个注意事项:当使用构建工具revision14 +时,不建议(不允许)使用带有非常量的switch-case。有关详细信息,请参阅此页:http://tools.android.com/recent/buildchangesinrevision14
警告:在库项目中生成ID为非final,表示库代码不能将这些ID视为常量。这意味着,例如,您不能在switch语句中使用res ID。
在您提供的代码中,您需要检查按钮ID(R.id.minusButton
等)。您应该将您的开关/案例更改为更像这样的内容:
if(v.getId() == R.id.addButton) {
count++;
tvCounter.setText(String.valueOf(count));
}
else if(v.getId() == R.id.minusButton) {
count--;
tvCounter.setText(String.valueOf(count));
}
else if(v.getId() == R.id.resetButton) {
count = 0;
tvCounter.setText(String.valueOf(count));
}
答案 1 :(得分:1)
在我看来,ID并非位于您的某个视图中。也许代替那个案例陈述只是做这样的事情。
if (butnAdd == v) {
} else if (btnMinus == v) {
} else if (btnReset == v) {
} else {
}
答案 2 :(得分:1)
使用以下代码替换整个onClick方法:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addButton:
count++;
break;
case R.id.minusButton:
count--;
break;
case R.id.resetButton:
count = 0;
break;
}
tvCounter.setText(String.valueOf(count));
}
答案 3 :(得分:0)
类型转换有错误,您将整数类型的数据设置为文本类型。