当我将ScrollView添加到ConstraintLayout时,应用程序崩溃

时间:2019-02-14 12:04:51

标签: android android-constraintlayout

当我尝试使约束布局可滚动时,我将约束布局包装在ScrollView中。我在XML布局中没有任何视图的情况下对其进行了测试,它可以正常工作(滚动视图有效,一切都很好)。但是,当我添加一些视图时(例如我发布的XML),它崩溃并显示此错误。 该错误将我指向main_activity,但我的main_activity很好(我也将其发布了)

这是错误消息:

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: athleanx.example.org.myapplication, PID: 9851
    java.lang.RuntimeException: Unable to start activity ComponentInfo{athleanx.example.org.myapplication/athleanx.example.org.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class ImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
        at android.app.ActivityThread.access$800(ActivityThread.java:170)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5568)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
     Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class ImageView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at athleanx.example.org.myapplication.MainActivity.onCreate(MainActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:6041)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556) 
        at android.app.ActivityThread.access$800(ActivityThread.java:170) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5568) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 
     Caused by: android.content.res.Resources$NotFoundException: Resource "athleanx.example.org.myapplication:drawable/train_like_athlete" (7f070097)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f070097 a=-1 r=0x7f070097}
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2601)
        at android.content.res.Resources.loadDrawable(Resources.java:2541)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
        at android.widget.ImageView.<init>(ImageView.java:146)
        at android.widget.ImageView.<init>(ImageView.java:135)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
        at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
        at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at athleanx.example.org.myapplication.MainActivity.onCreate(MainActivity.java:34) 
        at android.app.Activity.performCreate(Activity.java:6041) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556) 
        at android.app.ActivityThread.access$800(ActivityThread.java:170) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5568) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at 

这是我的content_main.xml

<ScrollView 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

 <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


     <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:background="#000000"
        android:padding="25dp"
        android:src="@drawable/train_like_athlete"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="16dp"

        android:background="#c40000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="40dp"
        android:src="@drawable/border_tablet"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view" />

     <TextView
         android:layout_width="match_parent"
         android:layout_height="48dp"
         android:layout_marginTop="16dp"
         android:text="@string/the_training"
         android:textAlignment="center"
         android:textColor="#000000"
         android:textSize="30sp"
         android:textStyle="bold"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="@+id/imageView4" />

     <TextView
         android:id="@+id/textView2"
         android:layout_width="match_parent"
         android:layout_height="55dp"
         android:layout_marginTop="20dp"
         android:text="@string/train_like_an_athlete"
         android:textAlignment="center"
         android:textColor="#000000"
         android:textSize="34sp"
         android:textStyle="bold"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/imageView4" />

     <TextView
         android:id="@+id/textView3"
         android:layout_width="match_parent"
         android:layout_height="56dp"
         android:layout_marginTop="4dp"
         android:text="@string/each_program_is_designed_to_get_you_looking_moving_and_feeling_like_an_athlete"
         android:textAlignment="center"
         android:textColor="#4e4e4e"
         android:textSize="15sp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/textView2" />

     <view
         android:id="@+id/view2"
         android:layout_width="match_parent"
         android:layout_height="250dp"
         android:layout_marginTop="15dp"
         android:background="#2d2d2d"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/textView3" />

     <TextView
         android:id="@+id/textView4"
         android:layout_width="match_parent"
         android:layout_height="92dp"
         android:layout_marginTop="36dp"
         android:paddingLeft="18dp"
         android:paddingRight="18dp"
         android:text="@string/CHOOSE_a_program"
         android:textAlignment="center"
         android:textColor="#ffffff"
         android:textSize="30sp"
         android:textStyle="bold"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toTopOf="@+id/view2" />

     <Button
         android:layout_width="210dp"
         android:layout_height="55dp"
         android:layout_marginTop="24dp"
         android:layout_marginEnd="100dp"
         android:layout_marginRight="100dp"
         android:background="#c40000"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/textView4" />

     </android.support.constraint.ConstraintLayout>

     </ScrollView> 

主要活动

 public class MainActivity extends AppCompatActivity {

athleanx.example.org.myapplication.ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();




        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        prepareListData();
        listAdapter = new athleanx.example.org.myapplication.ExpandableListAdapter(this, listDataHeader, listDataChild);

        expListView.setAdapter(listAdapter);

        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                if (id == 0) {
                    Intent myIntent = new Intent(expListView.getContext(), hello.class);
                    startActivityForResult(myIntent,0);
                }

                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }




    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Add header data
        //Add child data
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案:

 Caused by: android.content.res.Resources$NotFoundException: Resource "athleanx.example.org.myapplication:drawable/train_like_athlete" (7f070097)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f070097 a=-1 r=0x7f070097}

您附加到ImageView的可绘制对象出了点问题。

请注意使用:app:srcCompat而非android:srcCompatbuild.gradle(app)

将此行vectorDrawables.useSupportLibrary = true添加到defaultConfig { }