2010-09-03 158 views
1

我在我的应用程序中设置横向模式时遇到问题。切换到横向布局时应用程序崩溃

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

layout 
-----main.xml 

layout-land 
-----main.xml 

我/layout-land/main.xml含有比/layout/main.xml不同的UI元素/ res文件夹。如何在用户切换到横向模式时正确映射每个布局,反之亦然?

当用户切换到横向模式我基本上显示全屏ImageView的。 ImageView将从互联网下载图像并显示它。切换回肖像,应该回到我的肖像模式,它具有不同的UI组件。

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

,因为我不能得到ID:

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

chartImageViewLandscape是/layout-land/main.xml

我怎样才能得到这个参考?

回答

1

Sheehan,关于onRetainNonConfigurationInstance(),下面是我目前为我的应用程序正在进行的工作。我觉得我虽然过于复杂,但我认为有一种更简单的方法;然而,这工作得很好,我现在:

所以在我的活动类 “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)方法:

//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)); 
} 

再次,这就是我目前的做法。如果有人知道一个更有效的方法,无论如何评论。 :)

+0

我会尝试并将其实现到我自己的代码中!你知道我可以从我的/layout-land/main.xml中拉出chartImageViewLandscape吗?似乎它总是空! – 2010-09-03 22:07:56

+0

根据我的理解,您不希望实际存储View对象,这会导致内存泄漏。取而代之的是,存储ImageView包含的位图或drawable,然后将其设置回onCreate()方法中的chartImageViewLandscape视图。 – kcoppock 2010-09-03 22:10:02

+0

但我甚至无法设置chartImageViewLandScape,因为我需要能够通过findViewById将其挂接到布局 – 2010-09-03 22:11:57

1

问题是什么是什么呢?

你定义在两个不同的界面的android:orientation?除此之外,没有其他事情要做。 Android将自行切换。

如果你有不同的UI组件,您可能希望仍然宣布他们在这两种布局,以便于findViewById任何电话不会崩溃您的应用程序。只是要使他们不显示的布局(在FrameLayout里,例如背后的形象)

如果你喜欢更做手工,你需要把android:configChanges="orientation"在你的清单和实施onConfigurationChanged

+0

听起来不错。另外,就从互联网下载更改配置而言,你应该找到一种方法来在方向更改之间存储图像(对于我所做的一个操作,我使用onRetainNonConfigurationInstance()来保存对象,getLastNonConfigurationInstance ()来恢复图像),以便每次从纵向到横向都不会尝试重新下载图像,反之亦然。 – kcoppock 2010-09-03 18:51:53

+0

更新了我的问题的更多细节。 @kcoppock你能告诉我一个你的onRetainNonConfigurationInstance()的例子吗? – 2010-09-03 19:03:27

0

这是来自AdMob活性和具有多个参数例如screenSize适用于Android 3。2+

<activity android:name="com.google.ads.AdActivity" 
       android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 
0

转到你的Android“清单”的XML文件,并确保以下是存在的“活动”部分中:

android:configChanges="orientation|screenSize|keyboardHidden" 

舱单你最后的“活动”部分应类似于以下内容:

<activity 
    android:name=".MainActivity" 
    android:configChanges="orientation|screenSize|keyboardHidden" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity>