应用程序切换到横向布局时崩溃 [英] App crashes when switching to landscape layout

查看:125
本文介绍了应用程序切换到横向布局时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的应用程序中设置横向模式。

I am having trouble setting up landscape mode in my application.

我有一个包含布局文件夹和一个/ res文件夹布局土地文件夹

I have a /res folder that contains a layout folder and a layout-land folder

layout
-----main.xml

layout-land
-----main.xml

我/layout-land/main.xml含有比/layout/main.xml不同的UI元素。我该如何映射每个布局合理,当用户切换为横向模式,反之亦然?

My /layout-land/main.xml contains different UI elements than /layout/main.xml. How can I map each layout appropriately when the user has switched to landscape mode and vice versa?

当用户切换到横向模式我基本上显示一个全屏幕的ImageView。该ImageView的会从互联网上下载的图像,并显示它。切换回纵向,应该简单地回到我的肖像模式,其中有一组不同的UI组件。

I am basically displaying a full screen ImageView when the user switches to landscape mode. The ImageView will download an image from the internet and display it. Switching back to portrait, should simply go back to my portrait mode, which has a different set of UI components.

我得到一个崩溃时,我切换到横向模式:

I get a crash when I switch to landscape mode:

,因为我不能得到的ID:

because I can't get the id:

chartImageViewLandscape = (ImageView) this.findViewById(R.id.chartImageViewLandscape);

chartImageViewLandscape是/layout-land/main.xml

chartImageViewLandscape is in /layout-land/main.xml

我怎样才能得到一个参照本?

How can I get a reference to this?

推荐答案

希恩,对于onRetainNonConfigurationInstance(),下面是我目前做的事情对我正在进行的应用程序。我有我虽然它过于复杂的感觉,我觉得还有一个更简单的方法;然而,这工作得很好,我现在:

Sheehan, regarding onRetainNonConfigurationInstance(), below is what I am currently doing for my app in progress. I have the feeling I'm overcomplicating it though, I think there's a simpler way; however, this works just fine for me currently:

所以在我的活动课RotationMenu.java

So in my activity class "RotationMenu.java":

private Object catcher;
//lots of non-related code here

//directoryList is a returned list of selected directories, that I wish to
//retain in the event of an orientation state change.
String[] directoryList = new String[arrayList.size()];
arrayList.toArray(directoryList);

//here, I set the class Object catcher to the directoryList
catcher = directoryList;

//rest of non-related code

//this method is called when the orientation changes (for me,
//when I open my Droid's hardware keyboard)
public Object onRetainNonConfigurationInstance()
{
    //If I've entered anything into my catcher Object, it will be kept
    //across orientation changes.
    final Object data = catcher;
    return data;
}

现在,在我的onCreate(捆绑savedInstanceState)方法:

Now, in my onCreate(Bundle savedInstanceState) method:

//We retrieve the stored Object and cast it to a String array
final Object recipient = (String[]) getLastNonConfigurationInstance();

//in case the state changes again before the code that sets the directories is run
catcher = recipient;

//if there was any stored data, we can now reinstate the list adapter where the
//directoryList was originally being used.
if(recipient != null)
{
    returnedDirectories.setAdapter(new ArrayAdapter<String>(
        this.getBaseContext(),
        R.layout.simple_list_item_small,
        (String[])recipient));
}

此外,这是多么我目前做这件事。如果有人更有效的方法的人都知道,通过各种手段发表评论。 :)

Again, this is how I am doing it currently. If anyone knows of a more efficient method, by all means comment. :)

这篇关于应用程序切换到横向布局时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆