将imageview添加到collapsingtoolbarlayout Android时出错

时间:2018-02-14 03:23:37

标签: android

我想将图片放在我的应用栏布局上,如下所示

enter image description here

我已经在mipmap中使用了我的图像但是在向AppbarLayout添加ImageView时遇到了问题

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.example.android.imgedit.MainPage">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar"
            app:statusBarScrim="@null">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@mipmap/appbarimage"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>


    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main_page" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

然而,之后我遇到了一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.imgedit/com.example.android.imgedit.MainPage}: android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
                  at android.app.ActivityThread.access$1000(ActivityThread.java:164)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:160)
                  at android.app.ActivityThread.main(ActivityThread.java:5541)
                  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:964)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
               Caused by: android.view.InflateException: Binary XML file line #0: 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.rInflate(LayoutInflater.java:809)
                  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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.example.android.imgedit.MainPage.onCreate(MainPage.java:24)
                  at android.app.Activity.performCreate(Activity.java:6093)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577) 
                  at android.app.ActivityThread.access$1000(ActivityThread.java:164) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:160) 
                  at android.app.ActivityThread.main(ActivityThread.java:5541) 
                  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:964) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
               Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.android.imgedit:mipmap/appbarimage" (7f0c0000)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f0c0000 a=-1 r=0x7f0c0000}
                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
                  at android.content.res.Resources.loadDrawable(Resources.java:2538)
                  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.createView(AppCompatViewInflater.java:106)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
                  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.rInflate(LayoutInflater.java:809) 
                  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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at com.example.android.imgedit.MainPage.onCreate(MainPage.java:24) 
                  at android.app.Activity.performCreate(Activity.java:6093) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577) 
                  at android.app.ActivityThread.access$1000(ActivityThread.java:164) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:160) 
                  at android.app.ActivityThread.main(ActivityThread.java:5541) 
                  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:964) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 

有人知道这个的原因吗?

如果我删除了ImageView组件,一切正常

3 个答案:

答案 0 :(得分:0)

将您的图像文件放在可绘制文件中,并将代码更改为:

android:src="@drawable/appbarimage"

我认为它会起作用。

答案 1 :(得分:0)

错误说:

C:\Python27\python.exe C:/Users/abhis/PycharmProjects/IRR/irr.py Yearly -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00
Traceback (most recent call last):
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 74, in <module>
['-30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00']
    main()
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 64, in main
    irrCal(tenure, array)
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 49, in irrCal
    print(irrYearly(array))
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py"", line 19, in irrYearly
    irr = round(numpy.irr(array), 2)*100
  File "C:\Python27\lib\site-packages\numpy\lib\financial.py", line 669, in irr
    res = np.roots(values[::-1])
  File "C:\Python27\lib\site-packages\numpy\lib\polynomial.py", line 222, in roots
    p = p.astype(float)
ValueError: invalid literal for float(): -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00

确保ImageView没有正确充气。

确保将您的appbarimage复制到drawable文件夹中并更改

Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.android.imgedit:mipmap/appbarimage" (7f0c0000)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f0c0000 a=-1 r=0x7f0c0000}

android:src="@mipmap/appbarimage"

答案 2 :(得分:0)

我有一个类似的问题。只需确保图像位于android:src =“ @ drawable / appbarimage”