扩充类错误 - CircularImageView

时间:2015-09-12 21:45:07

标签: android android-studio libs

我试图在我的Android应用程序中使用this项目。

我的list_style.xml看起来像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/libs/circularimageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<com.mikhaellopez.circularimageview.CircularImageView
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/kamilwirtek"
    android:layout_gravity="left"
    android:layout_margin="10dp"
    android:id="@+id/avatar"
    app:border="true"
    app:border_color="#000000"
    app:border_width="4dp"
    app:shadow="true" />

<TextView
    android:id="@+id/full_name"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textColor="#111111"
    android:text="TextView" />

</LinearLayout>

当我正在运行应用程序时,我使用以下代码收到错误:

09-12 21:33:39.270    5506-5506/info.androidhive.materialdesign E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #10: Error inflating class com.mikhaellopez.circularimageview.CircularImageView
            at android.view.LayoutInflater.createView(LayoutInflater.java:613)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at info.androidhive.materialdesign.activity.CustomAdapter.getView(CustomAdapter.java:36)
            at android.widget.AbsListView.obtainView(AbsListView.java:2271)
            at android.widget.ListView.makeAndAddView(ListView.java:1769)
            at android.widget.ListView.fillDown(ListView.java:672)
            at android.widget.ListView.fillFromTop(ListView.java:733)
            at android.widget.ListView.layoutChildren(ListView.java:1622)
            at android.widget.AbsListView.onLayout(AbsListView.java:2106)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13754)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Nativ
09-12 21:35:22.970    5506-5510/info.androidhive.materialdesign D/dalvikvm﹕ GC_CONCURRENT freed 510K, 5% free 12177K/12807K, paused 23ms+8ms, total 69ms 

我正在使用Fragments。当我添加onCreateView时:

CircularImageView circularImageView = (CircularImageView)getActivity().findViewById(R.id.avatar);
circularImageView.setBorderColor(getResources().getColor(R.color.GrayLight));
circularImageView.setBorderWidth(10);
circularImageView.addShadow(); 

我的代码出现了另一个错误:

09-12 21:41:21.359    8783-8783/info.androidhive.materialdesign E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.materialdesign/info.androidhive.materialdesign.activity.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at info.androidhive.materialdesign.activity.HomeFragment.onCreateView(HomeFragment.java:42)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
            at android.app.Activity.performStart(Activity.java:5018)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

出了什么问题?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您正试图从活动的视图而不是片段中获取视图。

而不是:

CircularImageView circularImageView = (CircularImageView)getActivity().findViewById(R.id.avatar);

你应该做

CircularImageView circularImageView = (CircularImageView)view.findViewById(R.id.avatar);

片段&#39; onViewCreated(View,Bundle)