Landscape Orientation crashing Android app

ε祈祈猫儿з 提交于 2019-12-12 00:45:31

问题


Whenever I try to change the the orientation to landscape in the app I'm developing it crashes.

for portrait mode res/layout/activity_main.xml for landscape mode res/layout-land/activity_main.xml

I am using Linear-Layout but eclipse keeping showing "This LinearLayout layout or its LinearLayout parent is useless" . Below is the code of Landscape Mode. Please Help.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="15dip"
    android:orientation="horizontal" >
    <LinearLayout
        android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:paddingLeft="20dip"
    android:paddingRight="20dip">
        <TextView
        android:text="@string/main_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="20dip"
        android:textSize="24.5sp" />
        <TableLayout >
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:stretchColumns="*">
        <TableRow>
        <Button
        android:id="@+id/button1"
        android:text="@string/continue_label" />
        <Button
        android:id="@+id/button2"
        android:text="@string/new_game_label" />
        </TableRow>
        <TableRow>
        <Button
        android:id="@+id/button3"
        android:text="@string/about_label" />
        <Button
        android:id="@+id/button4"
        android:text="@string/exit_label" />
        </TableRow>
        </TableLayout>

    </LinearLayout>


</LinearLayout>

回答1:


use in your Manifest at your activity:

android:configChanges="orientation"

try to use then:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    Display display = ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    AddArticleActivity.DISPLAYROTATION = display.getRotation();
    if (youractivity.DISPLAYROTATION == Surface.ROTATION_90 || youractivity.DISPLAYROTATION == Surface.ROTATION_270) {
        do LANDSCAPE;
    } else {
        do PORTRAIT;
    }

}



回答2:


Add this with activity class in manifest file, If your android:targetSdkVersion="12" or less

android:configChanges="orientation|keyboardHidden">

if your android:targetSdkVersion="13" or more

 android:configChanges="orientation|keyboardHidden|screensize">



回答3:


about the error "This LinearLayout layout or its LinearLayout parent is useless"
it's because the second LinearLayout is indeed useless, you can remove it

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:orientation="horizontal" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:paddingLeft="20dip"
        android:paddingRight="20dip">

just change it to

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="15dip"
    android:paddingBottom="15dip"
    android:paddingLeft="20dip"
    android:paddingRight="20dip"
    android:orientation="vertical"
    android:layout_gravity="center" >

and remove one of </LinearLayout> in the bottom

unless you have another viewgroup below the second linearlayout, the warning will not appear..
and i think it's only a warning, not the main issue which crashes your app
you should look again at the logcat to check what exception makes the app crashes



来源:https://stackoverflow.com/questions/18034598/landscape-orientation-crashing-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!