2

Problem when editing portrait-to-landscape layouts

 2 years ago
source link: https://www.codesd.com/item/problem-when-editing-portrait-to-landscape-layouts.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Problem when editing portrait-to-landscape layouts

advertisements

I have two layouts one for portrait one for landscape. For portrait I have it under the layout folder and for landscape I have it under the layout-land folder. Both have the same name main.xml. The switching happens without any errors fro portrait to landscape. Again when I switch from landscape to portrait, for some reason its not loading the portrait based layout and is unable to find the root layout I have defined. Here's the Logcat output\

07-27 15:25:09.601: WARN/System.err(278): java.lang.ClassCastException:    android.widget.LinearLayout
07-27 15:25:10.230: WARN/System.err(278):     at  com.me2youmob.swagwrap.ChickenWrapActivity.loadMBIIntoView(ChickenWrapActivity.java:102)
07-27 15:25:10.230: WARN/System.err(278):     at com.me2youmob.swagwrap.ChickenWrapActivity.onCreate(ChickenWrapActivity.java:41)
07-27 15:25:10.230: WARN/System.err(278):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 15:25:10.250: WARN/System.err(278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-27 15:25:10.250: WARN/System.err(278):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-27 15:25:10.250: WARN/System.err(278):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
07-27 15:25:10.311: WARN/System.err(278):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
07-27 15:25:10.311: WARN/System.err(278):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
07-27 15:25:10.311: WARN/System.err(278):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 15:25:10.322: WARN/System.err(278):     at android.os.Looper.loop(Looper.java:123)
07-27 15:25:10.322: WARN/System.err(278):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 15:25:10.329: WARN/System.err(278):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 15:25:10.329: WARN/System.err(278):     at java.lang.reflect.Method.invoke(Method.java:521)
07-27 15:25:10.361: WARN/System.err(278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 15:25:10.361: WARN/System.err(278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 15:25:10.371: WARN/System.err(278):     at dalvik.system.NativeStart.main(Native Method)

The code that's there in onCreate is as follows

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int deviceRotation = getWindowManager().getDefaultDisplay().getRotation();
    Utils.spPreferences = getSharedPreferences(Utils.PREFS_NAME, 0);
    if (deviceRotation == 1)
    {
        int imgID = Utils.getMbiIndex("mbiIndex");
        int drawBgId = Utils.getMbiDrawIndex(imgID);
        try
        {
            ImageView ivLandView = (ImageView) findViewById(R.id.ivLandMbi);
            ivLandView.setImageResource(drawBgId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    else
    {
        loadMBIIntoView();
        handleButtonClicks();
    }
}

The error happenson the loadMBIIntoView method where the code is as follows

    public void loadMBIIntoView()
{
    int imgID = Utils.getMbiIndex("mbiIndex");
    int drawBgId = Utils.getMbiDrawIndex(imgID);
    try {
        RelativeLayout llMain = (RelativeLayout) findViewById(R.id.rlMain);
        Resources res = getResources();
        Drawable drawMbi = res.getDrawable(drawBgId);
        llMain.setBackgroundDrawable(drawMbi);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The landscape layout does not have rlMain layout itself but the portrait does. If the activity is being restarted(which is fine with me) everytime I change the screen mode shouldn't it automatically load the appropriate layouts ? I would appreciate any help on this. Thank You.

UPDATE : 02:00 PM

I added the code but its still give the same error. The error crops up because of the casting of a linear layout into a relative layout and this is where the issue is. The portrait layout("main.xml") is in its default "layout" folder. This layout has the relative layout("rlmain"). The landscape layout("main.xml") resides in the layout-land folder. This main.xml has different UI specifications and has the linear layout. By default I load in portrait mode where it loads fine and the I press Ctrl + F11 to change it to Landscape, the layout from layout-land folder loads fine. Then I press Ctrl + F11 again to change to Portrait where the main.xml from the default layout folder should have loaded but is not getting loaded which is why I am getting the case exception.


It seems like you are trying to cast a LinearLayout to a RelativeLayout, but that doesn't make much sense since it originally works in portrait mode. It might be a problem with your if statement in onCreate() though. It's possible that your loadMBIIntoView() function is called when it shouldn't be, causing the error.

You test for deviceRotation == 1, but that only catches the case where the device is rotated 90 degrees, ignoring the case where it is rotated 270 degrees.

Try this instead and see if anything changes:

if(deviceRotation == Surface.ROTATION_90 || deviceRotation == Surface.ROTATION_270)
{
    ...
}
else
{
    ...
}

Surface.ROTATION_90 is a pre-defined constant that equals 1, and Surface.ROTATION_270 equals 3.

EDIT:

I still think it's a problem with your deviceRotation. Ctrl + F11 goes to the "previous layout orientation" while Ctrl + F12 goes to the "next layout orientation" as described here. So when you press F11 twice you are not returning to the original orientation. You are switching to an upside down portrait mode.

Try pressing F11 the first time, and then F12 the second time, so that the emulator really does return to it's true original state and see if that still causes an error.

EDIT #2:

This appears to be a problem (possibly a bug) with emulator rotation. A similar question to this one can be found here.

Essentially, the emulator makes extra lifecycle calls that a real device doesn't. Supposedly it simulates rotation by opening the keyboard instead of through the accelerometer as a real device would.

If you can, I would test your code on a real device to see if it's a legitimate issue. Right now it seems like it's just a problem with the emulator and you should ignore it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK