将布局从纵向模式切换到横向模式时出现问题
我有两种布局,一种用于纵向,一种用于横向。对于纵向,我将其放在布局文件夹下,对于横向,我将其放在布局土地文件夹下。两者具有相同的名称 main.xml。从纵向到横向的切换没有任何错误。再次,当我从横向切换到纵向时,由于某种原因,它没有加载基于纵向的布局,并且无法找到我定义的根布局。这是 Logcat 输出\
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)
onCreate 中的代码如下
@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();
}
}
错误发生在 loadMBIIntoView 方法上,其中代码如下 横向
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();
}
}
布局本身没有 rlMain 布局,但纵向布局有。如果每次我更改屏幕模式时都重新启动活动(这对我来说很好),它不应该自动加载适当的布局吗?我将不胜感激任何帮助。谢谢。
更新:02:00 PM
我添加了代码,但它仍然给出相同的错误。由于将线性布局转换为相对布局而出现错误,这就是问题所在。纵向布局(“main.xml”)位于其默认的“layout”文件夹中。该布局具有相对布局(“rlmain”)。景观布局(“main.xml”)位于layout-land 文件夹中。此 main.xml 具有不同的 UI 规范并具有线性布局。默认情况下,我以纵向模式加载,加载良好,然后按 Ctrl + F11 将其更改为横向,layout-land 文件夹中的布局加载良好。然后我再次按 Ctrl + F11 更改为 Portrait,其中默认布局文件夹中的 main.xml 应该已加载但未加载,这就是我收到案例异常的原因。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在尝试将 LinearLayout 转换为relativelayout,但这没有多大意义,因为它最初在纵向模式下工作。不过,这可能是 onCreate() 中的 if 语句有问题。您的 loadMBIIntoView() 函数可能在不应该调用的时候被调用,从而导致错误。
您测试 deviceRotation == 1,但这仅捕获设备旋转 90 度的情况,忽略设备旋转 270 度的情况。
请尝试此操作,看看是否有任何变化:
Surface.ROTATION_90 是一个预定义常量,等于 1,Surface.ROTATION_270 等于 3。
编辑:
我仍然认为这是您的 deviceRotation 的问题。 Ctrl + F11 转到“上一个布局方向”,而 Ctrl + F12 转到“下一个布局方向”,如所述此处。因此,当您按两次 F11 时,您不会返回到原始方向。您正在切换到颠倒的纵向模式。
尝试第一次按 F11,然后第二次按 F12,以便模拟器真正返回到其真正的原始状态,并查看是否仍然会导致错误。
编辑#2:
这似乎是一个问题(可能是一个错误) 与模拟器旋转。
与此类似的问题可以在此处找到。
本质上,模拟器会进行额外的生命周期调用,而真实设备不会进行这些调用。据说它通过打开键盘而不是通过加速度计就像真实的设备一样。
如果可以的话,我会在真实设备上测试您的代码,看看这是否是一个合法的问题。现在看来这只是模拟器的问题,你应该忽略它。
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:
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.
我也面临同样的问题。就这样解决了。它的主要问题是当你从横向切换到纵向时,活动并不令人耳目一新。您可以做一件事,覆盖 onConfigurationChanged 并像这样实现。它将刷新活动。
它会对你有所帮助。如果您的问题得到解决,请回复。
I also faced the same problem. It got it resolved. The main problem with it is when you switch from landscape to portrait, the activity is not refreshing. You can do one thing, override the onConfigurationChanged and implement like this. It will refresh the activity.
It will help you. Please reply if your problem get solved.
将这些行 AndroidManifest.xml 添加到您想要指定方向的活动中。
android:configChanges="orientation|keyboardHidden|screenSize"
在代码中使用这些行进行方向更改
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
另请记住在 <"setContentView"> 之后设置方向作为
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Add these line AndroidManifest.xml to your activity which you want to give orientation.
android:configChanges="orientation|keyboardHidden|screenSize"
In Code use these lines for orientation change
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Also keep in mind to set orientation after <"setContentView"> as
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);