我正在尝试通过侧面的按钮增加和减少中间的文本视图。该应用程序可以正常启动,但是当我单击任何按钮时,它会因以下错误而关闭。
Error: process <package> has stopped unexpectedly.
我的 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="250dp"
android:text="+"
android:textSize="40dp" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="80dp"
android:layout_toRightOf="@+id/button1"
android:layout_marginTop="75dp"
android:layout_marginLeft="80dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:text="-"
android:textSize="40dp" />
我的java文件:
public class IncrementDecrementActivity extends Activity {
int counter;
Button add, sub;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.tv1);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
tv.setText(counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
tv.setText(counter);
}
});
}
}