【发布时间】:2015-06-01 00:40:07
【问题描述】:
我正在制作一个允许用户单击加号或减号图像按钮来更改文本视图中显示的数字的 android 应用程序。
有 18 个文本视图和每个文本视图的加号和减号图像按钮。
当我进入这个活动时,活动崩溃了,这是因为这段代码。在调试代码时,我无法提供任何有用的信息,因此我们将不胜感激,谢谢!
在我的活动顶部:
int score = 0;
ImageButton addButtons[] = {(ImageButton)findViewById(R.id.hole1Up), (ImageButton)findViewById(R.id.hole2Up), (ImageButton)findViewById(R.id.hole3Up), (ImageButton)findViewById(R.id.hole4Up), (ImageButton)findViewById(R.id.hole5Up), (ImageButton)findViewById(R.id.hole6Up), (ImageButton)findViewById(R.id.hole7Up), (ImageButton)findViewById(R.id.hole8Up), (ImageButton)findViewById(R.id.hole9Up), (ImageButton)findViewById(R.id.hole10Up), (ImageButton)findViewById(R.id.hole11Up), (ImageButton)findViewById(R.id.hole12Up), (ImageButton)findViewById(R.id.hole13Up), (ImageButton)findViewById(R.id.hole14Up), (ImageButton)findViewById(R.id.hole15Up), (ImageButton)findViewById(R.id.hole16Up), (ImageButton)findViewById(R.id.hole17Up), (ImageButton)findViewById(R.id.hole18Up),};
ImageButton minusButtons[] = {(ImageButton)findViewById(R.id.hole1Down), (ImageButton)findViewById(R.id.hole2Down), (ImageButton)findViewById(R.id.hole3Down), (ImageButton)findViewById(R.id.hole4Down), (ImageButton)findViewById(R.id.hole5Down), (ImageButton)findViewById(R.id.hole6Down), (ImageButton)findViewById(R.id.hole7Down), (ImageButton)findViewById(R.id.hole8Down), (ImageButton)findViewById(R.id.hole9Down), (ImageButton)findViewById(R.id.hole10Down), (ImageButton)findViewById(R.id.hole11Down), (ImageButton)findViewById(R.id.hole12Down), (ImageButton)findViewById(R.id.hole13Down), (ImageButton)findViewById(R.id.hole14Down), (ImageButton)findViewById(R.id.hole15Down), (ImageButton)findViewById(R.id.hole16Down), (ImageButton)findViewById(R.id.hole17Down), (ImageButton)findViewById(R.id.hole18Down),};
int[] scoreIDs = new int[] {R.id.score1, R.id.score2, R.id.score3, R.id.score4, R.id.score5, R.id.score6, R.id.score7, R.id.score8, R.id.score9, R.id.score10, R.id.score11, R.id.score12, R.id.score13, R.id.score14, R.id.score15, R.id.score16, R.id.score17, R.id.score18,};
public void setPlusButtons()
{
for(int i = 0; i < addButtons.length; i++)
{
final int j = i;
ImageButton button = addButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv5 = (TextView) findViewById(scoreIDs[j]);
tv5.setText(score++);
}
});
}
}
public void setMinusButtons()
{
for(int i = 0; i < minusButtons.length; i++)
{
final int j = i;
ImageButton button = minusButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv5 = (TextView) findViewById(scoreIDs[j]);
tv5.setText(score--);
}
});
}
}
我的 onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
Bundle extras = getIntent().getExtras();
if (extras != null) {
chosenCourseValue = extras.getString("passedChosenCourse");
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AsyncAPI APIThread = new AsyncAPI();
APIThread.execute();
setPlusButtons();
setMinusButtons();
}
我的活动 XML 部分
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Holes"
android:id="@+id/hole1"
android:layout_marginLeft="32dp"
android:textColor="#fff"
android:gravity="center"
android:background="@drawable/circle"
android:height="45dp"
android:width="45dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pars"
android:id="@+id/hole1Par"
android:paddingLeft="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/score1"
android:paddingLeft="42dp" />
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:id="@+id/hole1Up"
android:gravity="center"
android:layout_marginLeft="32dp"
android:layout_marginTop="6dp"
android:src="@drawable/plus"
android:background="@null" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:id="@+id/hole1Down"
android:gravity="center"
android:layout_marginLeft="26dp"
android:layout_marginTop="6dp"
android:src="@drawable/minus"
android:background="@null" />
</LinearLayout>
【问题讨论】:
-
我无法通过调试得到错误,所以希望有人能够指出我的代码做错了什么,请参阅第三段。
-
嘿,OJJ,我认为,您可以将
ListView or RecylerView与 cuttom 布局和适配器一起使用,而不是这样做。 -
你能发布
activity_coursexml布局文件吗?我有减少代码和代码复杂性的基本想法。 -
您好 user370305,我添加了部分 xml 布局(由于字数限制,无法全部上传)。基本上,其中有 18 个包装在线性布局中
标签: android for-loop onclick onclicklistener imagebutton