简体   繁体   English

更改为横向时,Android应用程序崩溃

[英]Android app crashes when changed to landscape orientation

I am developing a simple android app with a TextView and ImageView on the launching page. 我正在使用启动页面上的TextView和ImageView开发一个简单的android应用程序。 When I rotate my phone to landscape mode, the app crashes. 当我将手机旋转到横向模式时,应用程序崩溃。 Though I see that the app is not killed, it just goes away and I can see that it is still running in Android Studio. 尽管我看到该应用程序没有被杀死,但它消失了,而且我仍可以看到它仍在Android Studio中运行。 Following is my code: 以下是我的代码:

Content_main.xml -landscape mode Content_main.xml-风景模式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.viral.myapplication2.MainActivity"
    tools:showIn="@layout/activity_main">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView2" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ganpati_bappa_moraya"
        android:id="@+id/textView"
        tools:context=".Blink"
        android:textAlignment="center"
        android:textSize="@dimen/abc_action_button_min_width_overflow_material"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/god_pic"
        android:layout_below="@+id/textView" />
</RelativeLayout>

Content_main.xml - for normal mode - portrait Content_main.xml-用于普通模式-纵向

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.viral.myapplication2.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView2" />

    <TextView
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:text="Ganpati Bappa Moraya !!!"
        android:id="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        tools:context=".Blink"
        android:layout_marginTop="84dp"
        android:textAlignment="center"
        android:textSize="@dimen/abc_action_button_min_width_overflow_material" />

    <ImageView
        android:layout_width="500dp"
        android:layout_height="200dp"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:src="@mipmap/god_pic" />
</RelativeLayout>

Android_Manifest.xml file Android_Manifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.viral.myapplication2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/god_pic"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Modify AndroidManifest.xml file to this 将AndroidManifest.xml文件修改为此

android:configChanges="keyboardHidden|orientation|screenSize".

screenSize property will take care about screen orientation change and activity life cycle will not call again. screenSize属性将注意屏幕方向的更改,并且活动生命周期将不会再次调用。

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

相关问题 强制横向移动时,Android应用程序(嵌入式WebView)崩溃 - Android app (embedded WebView) crashes when forcing landscape orientation 当方向更改为横向时,Android 区域设置会更改 - Android locale gets changed when the orientation is changed to landscape Android-更改方向后未使用横向布局 - Android - Landscape Layout not being used when Orientation is changed Android Landscape to portrait在方向改变时不加载屏幕 - Android Landscape to portrait not loading screens when orientation changed 方向更改时Android应用程序崩溃 - Android app crashes when the orientation changes 更改方向后应用程序崩溃 - Application crashes when orientation is changed 更改方向时,Android应用会关闭 - Android app closes when the orientation is changed 更改屏幕方向后,Android应用终止 - Android app terminates when screen orientation is changed 风景定向崩溃的Android应用 - Landscape Orientation crashing Android app 当我将方向肖像更改为横向时,我希望布局应针对横向进行更改,并且视频是从android中的相同位置开始的? - when i changed orientation portrait to landscape i want layout should change for landscape and video is start from same position in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM