我的应用因不同的屏幕尺寸而崩溃

时间:2018-10-04 12:44:32

标签: android crash size screen responsive

我为“ layout-normal-land-1920x1080”做了一个android活动,但是每次我尝试运行它时都会崩溃,它仅适用于默认的xml文件,但是布局针对不同的屏幕尺寸而改变,我希望该活动如果有一种方法可以使自适应Android应用?? folders of layout for different screen sizes,则可以针对所有不同的屏幕尺寸和尺寸以相同的UI运行 screenshots for the previews of xml files(yellow buttons)

Android清单:

<supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens= "true"
    android:anyDensity="true"
    />

默认xml代码:     

<android.support.constraint.ConstraintLayout 
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:background="#ffffff"
android:orientation="horizontal"
tools:context=".TicketActivity"
tools:layout_editor_absoluteY="73dp">


<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.25"  />

<android.support.constraint.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.50"
    />

<android.support.constraint.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.75"
    />

<Button
    android:id="@+id/butt1"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
    android:id="@+id/butt6"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
    android:id="@+id/butt11"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
    android:id="@+id/butt2"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintStart_toEndOf="@+id/butt1"
    app:layout_constraintTop_toTopOf="@+id/guideline"


    app:layout_constraintVertical_bias="0.49" />

<Button
    android:id="@+id/butt7"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintStart_toEndOf="@+id/butt6"
    app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
    android:id="@+id/butt12"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/butt11"
    app:layout_constraintTop_toTopOf="@+id/guideline3"
    app:layout_constraintVertical_bias="0.619" />

<Button
    android:id="@+id/butt3"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="7dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintStart_toEndOf="@+id/butt2"

    app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
    android:id="@+id/butt8"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="7dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintStart_toEndOf="@+id/butt7"
    app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
    android:id="@+id/butt13"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="7dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/butt12"
    app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
    android:id="@+id/butt4"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintStart_toEndOf="@+id/butt3"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
    android:id="@+id/butt9"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintStart_toEndOf="@+id/butt8"
    app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
    android:id="@+id/butt14"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    android:background="#ffff0f"

    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/butt13"
    app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
    android:id="@+id/butt5"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"

    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintStart_toEndOf="@+id/butt4"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    android:background="#ffff0f"
    android:textSize="15sp"
    android:textStyle="bold"

    />

<Button
    android:id="@+id/butt10"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintStart_toEndOf="@+id/butt9"
    app:layout_constraintTop_toTopOf="@+id/guideline2"
    android:background="#ffff0f"
    android:textSize="15sp"
    android:textStyle="bold"
    />

<Button
    android:id="@+id/butt15"
    android:layout_width="110dp"
    android:layout_height="63dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/butt14"
    app:layout_constraintTop_toTopOf="@+id/guideline3"
    android:background="#ffff0f"
    android:textSize="15sp"
    android:textStyle="bold"
    />

<TextView
    android:id="@+id/themeTxt"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:layout_marginTop="15dp"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="15pt"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

错误:

Process: com.example.asad.tdk, PID: 3667
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asad.tdk/com.example.asad.tdk.TicketActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001e
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001e
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2101)
    at android.content.res.Resources.getLayout(Resources.java:1115)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.example.asad.tdk.TicketActivity.onCreate(TicketActivity.java:76)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:0)

要为不同的屏幕分辨率维护您的应用,您可能必须使用多个布局或多个dimens.xml文件。 基本上,我们维护
340 X 480-mdpi
540 X 960-hdpi
720 X 1280- Xhdpi
1080 X 1920- XXhdpi(1080 X 2160、1080 X 2200全屏显示)
1440 X 2880- XXXhdpi
6.5至7.5英寸标签-sw600dp
7.5-10.1英寸Tab-SW720DP
大于sw800dp

如果您要维护多个设备的布局,请
为上述分辨率名称(即 layout-xxhdpi,layout-xxxhdpi )创建xml。对于风景- layout-land-hdpi

但是android应用无法实时响应。您可以做其他事情,例如设计340 X 480,即mdpi。让Android系统使用DP计算来扩展它。这样,您的用户界面将对手机有所响应。