Android应用仅适用于部分手机

时间:2018-01-30 19:16:04

标签: android xml android-layout layout-inflater

所以几天前我终于完成了我的第一个Android应用程序。当我开发应用程序时,我使用了我的Sony Xperia Z(Android 5.1.1,API 22)作为测试手机。到目前为止一切正常。直到我在三星galaxy J1(Android 4.4.4,API 19)上构建了应用程序。

minSdkVersion 19

targetSdkVersion 25

buildToolsVersion“25.0.3”

出于某种原因,我在三星手机上运行应用程序时出现此错误:

01-30 20:00:14.569 11914-11914/com.heli.minvekeplan E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.heli.minvekeplan, PID: 11914
          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heli.minvekeplan/com.heli.minvekeplan.ToolbarActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class Button
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                 at android.app.ActivityThread.access$800(ActivityThread.java:166)
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                 at android.os.Handler.dispatchMessage(Handler.java:102)
                 at android.os.Looper.loop(Looper.java:136)
                 at android.app.ActivityThread.main(ActivityThread.java:5584)
                 at java.lang.reflect.Method.invokeNative(Native Method)
                 at java.lang.reflect.Method.invoke(Method.java:515)
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                 at dalvik.system.NativeStart.main(Native Method)
                                                                   Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class Button
                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                 at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24)
                 at android.app.Activity.performCreate(Activity.java:5442)
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                 at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:136) 
                 at android.app.ActivityThread.main(ActivityThread.java:5584) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                  at dalvik.system.NativeStart.main(Native Method) 
                                                                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_settings_grey_24dp.xml from drawable resource ID #0x7f020074
                  at android.content.res.Resources.loadDrawable(Resources.java:3451)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:614)
                  at android.view.View.<init>(View.java:3585)
                  at android.widget.TextView.<init>(TextView.java:696)
                  at android.widget.Button.<init>(Button.java:107)
                  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
                  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                 at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                 at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24) 
                 at android.app.Activity.performCreate(Activity.java:5442) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                 at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:136) 
                 at android.app.ActivityThread.main(ActivityThread.java:5584) 
                 at java.lang.reflect.Method.invokeNative(Native Method) 
                 at java.lang.reflect.Method.invoke(Method.java:515) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                 at dalvik.system.NativeStart.main(Native Method) 
                                                                   Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
                at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:969)
                at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
                at android.content.res.Resources.loadDrawable(Resources.java:3447)
                at android.content.res.TypedArray.getDrawable(TypedArray.java:614) 
                at android.view.View.<init>(View.java:3585) 
                at android.widget.TextView.<init>(TextView.java:696) 
                at android.widget.Button.<init>(Button.java:107) 
                at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66) 
                at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62) 
                at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109) 
                at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029) 
                at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087) 
                at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) 
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24) 
                at android.app.Activity.performCreate(Activity.java:5442) 
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                at android.os.Handler.dispatchMessage(Handler.java:102) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5584) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                at dalvik.system.NativeStart.main(Native Method)

所以我在互联网上搜索并找到了这些答案:answer1answer2,但它们似乎都没有效果。我认为它有drawables和我的按钮使用我的drawables做的东西,但我不知道如何解决这个问题。谢谢你的帮助!

我的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"
tools:context="com.heli.minvekeplan.ToolbarActivity">

<com.heli.minvekeplan.CustomViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/masterviewpager">
</com.heli.minvekeplan.CustomViewPager>

<FrameLayout android:layout_width="match_parent"
    android:layout_marginTop="539dp"
    android:layout_height="match_parent">

    <View android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_dropshadow"/>
</FrameLayout>

<Button
    android:id="@+id/settings1"
    android:layout_width="38dp"
    android:layout_height="38dp"
    android:background="@drawable/ic_settings_grey_24dp"
    android:layout_marginTop="544dp"
    android:layout_marginStart="270dp"/>

<Button
    android:id="@+id/timeplan1"
    android:layout_width="38dp"
    android:layout_height="38dp"
    android:background="@drawable/ic_assignment_grey_24dp"
    android:layout_marginStart="45dp"
    android:layout_marginTop="544dp" />

<Button
    android:id="@+id/lekse1"
    android:layout_width="38dp"
    android:layout_height="38dp"
    android:background="@drawable/ic_edit_blue_24dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="540dp"/>

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="@color/colorToolbar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >


</android.support.v7.widget.Toolbar>


<TextView
    android:id="@+id/settingstext"
    android:layout_width="60dp"
    android:layout_height="15dp"
    android:layout_marginTop="578dp"
    android:layout_marginStart="260dp"
    android:text="Innstillingar"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="10sp" />

<TextView
    android:id="@+id/lekseplantext"
    android:layout_width="60dp"
    android:layout_height="15dp"
    android:layout_marginTop="578dp"
    android:layout_centerHorizontal="true"
    android:text="Lekseplan"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="10sp" />

<TextView
    android:id="@+id/timeplantext"
    android:layout_width="60dp"
    android:layout_height="15dp"
    android:layout_marginTop="578dp"
    android:layout_marginStart="35dp"
    android:text="Timeplan"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="10sp" />

我的活动:

package com.heli.minvekeplan;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class ToolbarActivity extends AppCompatActivity {

SectionsPagerAdapter mSectionsPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    final CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.masterviewpager);
    viewPager.setPagingEnabled(false);
    viewPager.setAdapter(mSectionsPagerAdapter);

    final TextView settingstext = (TextView) findViewById(R.id.settingstext);
    final TextView leksetext = (TextView) findViewById(R.id.lekseplantext);
    final TextView timetext = (TextView) findViewById(R.id.timeplantext);

    leksetext.setTextColor(Color.parseColor("#FF1FBA97"));
    settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));
    timetext.setTextColor(Color.parseColor("#FF5C5C5C"));

    final Button time = (Button) findViewById(R.id.timeplan1);
    final Button settings = (Button) findViewById(R.id.settings1);
    final Button lekse = (Button) findViewById(R.id.lekse1);

    SharedPreferences startt = this.getSharedPreferences("start", Context.MODE_PRIVATE);
    final String Start = startt.getString("start", "");

    if (Start.equals("settings")) {
        viewPager.setCurrentItem(2);
        settings.setBackgroundResource(R.drawable.ic_settings_blue_24dp);
        settingstext.setTextColor(Color.parseColor("#FF1FBA97"));

        lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
        leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));

        SharedPreferences.Editor editor = startt.edit();
        editor.putString("start", "RobloxGamer47");
        editor.apply();
    }
    else {

        viewPager.setCurrentItem(1);
    }

    lekse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lekse.setBackgroundResource(R.drawable.ic_edit_blue_24dp);
            leksetext.setTextColor(Color.parseColor("#FF1FBA97"));

            time.setBackgroundResource(R.drawable.ic_assignment_grey_24dp);
            timetext.setTextColor(Color.parseColor("#FF5C5C5C"));

            settings.setBackgroundResource(R.drawable.ic_settings_grey_24dp);
            settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));

            viewPager.setCurrentItem(1);
        }
    });


    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            settings.setBackgroundResource(R.drawable.ic_settings_blue_24dp);
            settingstext.setTextColor(Color.parseColor("#FF1FBA97"));

            lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
            leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));

            time.setBackgroundResource(R.drawable.ic_assignment_grey_24dp);
            timetext.setTextColor(Color.parseColor("#FF5C5C5C"));

            viewPager.setCurrentItem(2);
        }
    });


    time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            time.setBackgroundResource(R.drawable.ic_assignment_blue_24dp);
            timetext.setTextColor(Color.parseColor("#FF1FBA97"));

            lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
            leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));

            settings.setBackgroundResource(R.drawable.ic_settings_grey_24dp);
            settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));

            viewPager.setCurrentItem(0);
        }
    });

}
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                TimeActivity timeActivity = new TimeActivity();
                return timeActivity;
            case 1:
                LekseActivity lekseActivity = new LekseActivity();
                return lekseActivity;
            case 2:
                SettingsActivity settingsActivity = new SettingsActivity();
                return settingsActivity;

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

  }
}

可绘制的例子:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="38dp"
    android:height="38dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#1fba97"
    android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>

谢谢

1 个答案:

答案 0 :(得分:1)

您使用Vector Drawable作为按钮的背景。

Android API 21+完全支持此功能。因此,在使用API​​ 22的设备上运行时,您的应用程序可以正常工作。

要在API 21下使用矢量绘图,您需要在java / gradle代码中添加一些内容,然后它才能正常工作。

参见本指南:
https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html#vector-drawables-backward-solution