why am I getting NullPointerException when using m

2019-07-24 20:26发布

问题:

I am a beginner in android development, I am using moondroid CoverFlow (external library) to create a CoverFlow, but I don't get why I am getting a null pointer exception when using it in XML.

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:coverflow="http://schemas.android.com/tools">

   <it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow
      android:id="@+id/coverflow"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      coverflow:coverHeight="@dimen/cover_height"
      coverflow:coverWidth="@dimen/cover_width"
      coverflow:maxScaleFactor="1.5"
      coverflow:reflectionGap="0px"
      coverflow:rotationThreshold="0.5"
      coverflow:scalingThreshold="0.5">

   </it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow>

</RelativeLayout>

And here is the error that I am getting is:

java.lang.NullPointerExceptio at it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow.dispatchDraw(FeatureCoverFlow.java:471)
at android.view.View.draw(View.java:16974)   at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3764)   at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61)   at android.view.ViewGroup.drawChild(ViewGroup.java:3764)   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)   at android.view.View.draw(View.java:16974)   at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3764)   at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61)   at android.view.ViewGroup.drawChild(ViewGroup.java:3764)   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)   at android.view.View.draw(View.java:16974)   at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3764)   at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61)   at android.view.ViewGroup.drawChild(ViewGroup.java:3764)   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)   at android.view.View.draw(View.java:16974)   at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3764)   at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61)   at android.view.ViewGroup.drawChild(ViewGroup.java:3764)   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)   at android.view.View.draw(View.java:17071)

回答1:

Just take a look at the sample example it is set like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:coverflow="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="@android:color/darker_gray"
tools:context="it.moondroid.carousellayoutdemo.CoverFlowActivity">


<it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow
    android:id="@+id/coverflow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    coverflow:coverHeight="@dimen/cover_height"
    coverflow:coverWidth="@dimen/cover_width"
    coverflow:maxScaleFactor="1.5"
    coverflow:reflectionGap="0px"
    coverflow:rotationThreshold="0.5"
    coverflow:scalingThreshold="0.5"
    coverflow:spacing="0.6" />

<TextSwitcher
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:layout_alignParentBottom="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

and add some additional file like this Just take a look at the sample too