单个Android Activity中的多个ImageButtons

时间:2015-04-01 19:21:30

标签: java android-studio

我正在创建一个需要多个图像按钮的应用。如果我加载的图片数量超过1,则应用程序停止响应。 我没有得到任何错误消息,它只是停止工作? 这是我正在使用的代码:

    public class ItemMatch extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_match);


    ImageButton Ariel = (ImageButton)findViewById(R.id.imageButtonAriel);
    ImageButton Belle = (ImageButton)findViewById(R.id.imageButtonBelle);

    //*****SETTING IMAGES TO BUTTONS*****
    Ariel.setImageResource(R.drawable.ariel);
    Belle.setImageResource(R.drawable.belle);

    //******LISTENERS FOR IMAGE BUTTONS*****
    Ariel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast aTest = Toast.makeText(getApplicationContext(), "Ariel!", Toast.LENGTH_SHORT);
            aTest.show();
        }
    });

    Belle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast aTest = Toast.makeText(getApplicationContext(), "Belle!", Toast.LENGTH_SHORT);
            aTest.show();
        }
    });



    //*****COUNTDOWN TIMER*****
    final TextView text1=(TextView)findViewById(R.id.textView2);
    final TextView score =(TextView)findViewById(R.id.textView3);

    new CountDownTimer(30000,1000){

        @Override
        public void onTick(long millisUntilFinished) {
            text1.setText("Time Left: " + millisUntilFinished /1000 + "s");
            score.setText("Score: ");
        }

        @Override
        public void onFinish() {
            text1.setText("GAME OVER!");

        }
    }.start();



}

这是我的XML:

    <TextView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text=""
    android:id="@+id/textView2"
    android:clickable="false"
    android:textAlignment="center"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text=""
    android:id="@+id/textView3"
    android:layout_alignTop="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:scaleType="fitXY"
    android:id="@+id/imageButtonAriel"
    android:layout_above="@+id/imageButtonBelle"
    android:layout_alignRight="@+id/textView2"
    android:layout_alignEnd="@+id/textView2" />

<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:scaleType="fitXY"
    android:id="@+id/imageButtonBelle"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/imageButtonAriel"
    android:layout_alignStart="@+id/imageButtonAriel" />

<ImageView
    android:layout_width="200dp"
    android:layout_height="250dp"
    android:scaleType="fitXY"
    android:src="@drawable/belle"
    android:id="@+id/imageView"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true" />

0 个答案:

没有答案