简体   繁体   English

风景定向崩溃的Android应用

[英]Landscape Orientation crashing Android app

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 用于纵向模式res / layout / activity_main.xml用于横向模式res / layout-land / activity_main.xml

I am using Linear-Layout but eclipse keeping showing "This LinearLayout layout or its LinearLayout parent is useless" . 我使用的是Linear-Layout,但是日食不断显示“此LinearLayout布局或其LinearLayout父级无用”。 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>

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

}

Add this with activity class in manifest file, If your android:targetSdkVersion="12" or less 如果您的android:targetSdkVersion =“ 12”或更少,请在清单文件中将其与活动类一起添加

android:configChanges="orientation|keyboardHidden">

if your android:targetSdkVersion="13" or more 如果您的android:targetSdkVersion =“ 13”或更多

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

about the error "This LinearLayout layout or its LinearLayout parent is useless" 关于错误“此LinearLayout布局或其LinearLayout父级无用”
it's because the second LinearLayout is indeed useless, you can remove it 这是因为第二个LinearLayout确实没有用,您可以将其删除

<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 并删除底部的</LinearLayout>之一

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 您应该再次查看logcat以检查导致应用崩溃的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM