我是Android应用程序开发的新手...我所做的就是创建一个应用程序,在计数器中添加一个单击添加按钮,单击子单击子按钮。请指出我的错误
startingpoint.java `
package vignesh.pandian.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Startingpoint extends Activity {
int counter;
Button add, subb;
TextView displ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startingpoint);
counter=0;
add = (Button) findViewById(R.id.add);
subb =(Button) findViewById(R.id.bSub);
displ =(Button) findViewById(R.id.tex);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
displ.setText("value is "+counter);
}
});
subb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
displ.setText("value is"+counter);
}
});
}
}
activity_startingpoint.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Add one " />
<TextView
android:id="@+id/My"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:text="Vignesh's app !!!!"
tools:context=".Startingpoint" />
<TextView
android:id="@+id/tex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="35dp"
android:text="Value is"
android:textSize="35dp"
tools:context=".Startingpoint" />
<Button
android:id="@+id/bSub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/add"
android:layout_marginTop="37dp"
android:text="Subract one " />
</RelativeLayout>
答案 0 :(得分:2)
TextView displ;
displ =(Button)findViewById(R.id.tex);
这是错的,请使用此
displ =(TextView) findViewById(R.id.tex);
disp1是Textview对象,而不是按钮对象。