-1

我是 android 开发的新手(我是 ac#asp.net 家伙)正在尝试编写一个与 api10 及更高版本兼容的应用程序,并且我在 if then else 语句中使用不同的 xml 处理屏幕方向。我在每个 xml 中有 6 个 imageView 控件(名为 imageView1 - imageView6),它们是 onClick 事件(我不喜欢 imagebutton 在图像上放置填充的方式)。这样做的原因是我必须根据方向更改图像大小,因此总共有 12 张图像(如果包括高亮图像,则为 24 张)。

如果我将两个 xmls imageViews 作为 imageView1 留给 imageView6,应用程序不会崩溃,但是,我无法独立处理 12 个图像中的每一个。我只和他们中的 6 个一起工作。当我将 xml 的 imageView 更改为 imageView7 到 imageView12 时,应用程序完全崩溃,我不知道为什么?这似乎是我忽略了一些非常简单的东西,但我不知道它是什么。这是我的代码:

package com.firstmelissa.FirstBaptistMelissa;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

ImageView display1
ImageView display2;
ImageView display3;
ImageView display4;
ImageView display5;
ImageView display6;
ImageView display7;
ImageView display8;
ImageView display9;
ImageView display10;
ImageView display11;
ImageView display12;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_layout, menu);
        return true;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display getOrient = getWindowManager().getDefaultDisplay();
        if (getOrient != null)
        {
            int orientation = getResources().getConfiguration().orientation;   
            if (orientation == 1) {
                // Portrait
                setContentView(R.layout.main_layout);

            display1 = (ImageView) findViewById(R.id.imageView1);
            ImageView image1 = (ImageView) findViewById(R.id.imageView1);
            display2 = (ImageView) findViewById(R.id.imageView2);
            ImageView image2 = (ImageView) findViewById(R.id.imageView2);
            display3 = (ImageView) findViewById(R.id.imageView3);
            ImageView image3 = (ImageView) findViewById(R.id.imageView3);
            display4 = (ImageView) findViewById(R.id.imageView4);
            ImageView image4 = (ImageView) findViewById(R.id.imageView4);
            display5 = (ImageView) findViewById(R.id.imageView5);
            ImageView image5 = (ImageView) findViewById(R.id.imageView5);
            display6 = (ImageView) findViewById(R.id.imageView6);
            ImageView image6 = (ImageView) findViewById(R.id.imageView6);

            image1.setOnClickListener(this);
            image2.setOnClickListener(this);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
            image5.setOnClickListener(this);
            image6.setOnClickListener(this);

        }
        else if (orientation == 2) {
            // Landscape
            setContentView(R.layout.main_landscape);
            //Intent intent = new Intent(this, MainActivityland.class);
            //this.startActivity(intent);

            display7 = (ImageView) findViewById(R.id.imageView7);
            ImageView image7 = (ImageView) findViewById(R.id.imageView7);
            display8 = (ImageView) findViewById(R.id.imageView8);
            ImageView image8 = (ImageView) findViewById(R.id.imageView8);
            display9 = (ImageView) findViewById(R.id.imageView9);
            ImageView image9 = (ImageView) findViewById(R.id.imageView9);
            display10 = (ImageView) findViewById(R.id.imageView10);
            ImageView image10 = (ImageView) findViewById(R.id.imageView10);
            display11 = (ImageView) findViewById(R.id.imageView11);
            ImageView image11 = (ImageView) findViewById(R.id.imageView11);
            display12 = (ImageView) findViewById(R.id.imageView12);
            ImageView image12 = (ImageView) findViewById(R.id.imageView12);


            image7.setOnClickListener(this);
            image8.setOnClickListener(this);
            image9.setOnClickListener(this);
            image10.setOnClickListener(this);
            image11.setOnClickListener(this);
            image12.setOnClickListener(this);
            }
}  
    }

public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
        case R.id.imageView1:
        Toast.makeText(MainActivity.this, "This is Short Notification",
        Toast.LENGTH_SHORT).show();
        display1.setImageResource(R.drawable.ignitebuttonh);
        display2.setImageResource(R.drawable.strivebutton);
        display3.setImageResource(R.drawable.adultsbutton);
        display4.setImageResource(R.drawable.marriedsbutton);
        display5.setImageResource(R.drawable.womansbutton);
        display6.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView2:
        display2.setImageResource(R.drawable.strivebuttonh);
        display1.setImageResource(R.drawable.ignitebutton);
        display3.setImageResource(R.drawable.adultsbutton);
        display4.setImageResource(R.drawable.marriedsbutton);
        display5.setImageResource(R.drawable.womansbutton);
        display6.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView3:
        display3.setImageResource(R.drawable.adultsbuttonh);
        display2.setImageResource(R.drawable.strivebutton);
        display1.setImageResource(R.drawable.ignitebutton);
        display4.setImageResource(R.drawable.marriedsbutton);
        display5.setImageResource(R.drawable.womansbutton);
        display6.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView4:
        display4.setImageResource(R.drawable.marriedsbuttonh);
        display3.setImageResource(R.drawable.adultsbutton);
        display2.setImageResource(R.drawable.strivebutton);
        display1.setImageResource(R.drawable.ignitebutton);
        display5.setImageResource(R.drawable.womansbutton);
        display6.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView5:
        display5.setImageResource(R.drawable.womansbuttonh);
        display4.setImageResource(R.drawable.marriedsbutton);
        display3.setImageResource(R.drawable.adultsbutton);
        display2.setImageResource(R.drawable.strivebutton);
        display1.setImageResource(R.drawable.ignitebutton);
        display6.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView6:
        display6.setImageResource(R.drawable.sermonbuttonh);
        display5.setImageResource(R.drawable.womansbutton);
        display4.setImageResource(R.drawable.marriedsbutton);
        display3.setImageResource(R.drawable.adultsbutton);
        display2.setImageResource(R.drawable.strivebutton);
        display1.setImageResource(R.drawable.ignitebutton);
    case R.id.imageView7:
        Toast.makeText(MainActivity.this, "This is Short Notification",
        Toast.LENGTH_SHORT).show();
        display7.setImageResource(R.drawable.ignitebuttonh);
        display8.setImageResource(R.drawable.strivebutton);
        display9.setImageResource(R.drawable.adultsbutton);
        display10.setImageResource(R.drawable.marriedsbutton);
        display11.setImageResource(R.drawable.womansbutton);
        display12.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView8:
        display8.setImageResource(R.drawable.strivebuttonh);
        display7.setImageResource(R.drawable.ignitebutton);
        display9.setImageResource(R.drawable.adultsbutton);
        display10.setImageResource(R.drawable.marriedsbutton);
        display11.setImageResource(R.drawable.womansbutton);
        display12.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView9:
        display9.setImageResource(R.drawable.adultsbuttonh);
        display8.setImageResource(R.drawable.strivebutton);
        display7.setImageResource(R.drawable.ignitebutton);
        display10.setImageResource(R.drawable.marriedsbutton);
        display11.setImageResource(R.drawable.womansbutton);
        display12.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView10:
        display10.setImageResource(R.drawable.marriedsbuttonh);
        display9.setImageResource(R.drawable.adultsbutton);
        display8.setImageResource(R.drawable.strivebutton);
        display7.setImageResource(R.drawable.ignitebutton);
        display11.setImageResource(R.drawable.womansbutton);
        display12.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView11:
        display11.setImageResource(R.drawable.womansbuttonh);
        display10.setImageResource(R.drawable.marriedsbutton);
        display9.setImageResource(R.drawable.adultsbutton);
        display8.setImageResource(R.drawable.strivebutton);
        display7.setImageResource(R.drawable.ignitebutton);
        display12.setImageResource(R.drawable.sermonbutton);
        break;
    case R.id.imageView12:
        display12.setImageResource(R.drawable.sermonbuttonh);
        display11.setImageResource(R.drawable.womansbutton);
        display10.setImageResource(R.drawable.marriedsbutton);
        display9.setImageResource(R.drawable.adultsbutton);
        display8.setImageResource(R.drawable.strivebutton);
        display7.setImageResource(R.drawable.ignitebutton);
        break;
    }       
}

}

4

1 回答 1

1

知道这很简单!case R.id.imageView6:这是and之间缺少的 break 语句case R.id.imageView7:!这完全解决了我的问题,感谢您帮助定位它!

于 2012-08-04T16:49:22.923 回答