ListView动画上的NullPointerError

时间:2013-09-18 20:17:26

标签: android android-listview android-animation

我在ListView内嵌了LinearLayout FrameLayout。我能够在ObjectAnimation's内对其他视图执行LinearLayout,但在ListView上执行动画时会收到空指针错误。随附的是详细信息和错误日志:

有问题的ListView在附加的布局中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- CalendarView and ListView -->
    <LinearLayout
        android:id="@+id/layout_contents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:animateLayoutChanges="true" >

        <CalendarView
          android:id="@+id/calendarView1"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="3"
          android:background="@drawable/color_border"
         />

        <RelativeLayout
            android:id="@+id/button_relative"
            android:layout_width="match_parent"
            android:layout_height="0dp"
          android:layout_weight="0.3"
            android:background="#807FE817" >

            <ImageButton
                android:id="@+id/change_cols_btn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/ic_btn_round_more_normal" />

        </RelativeLayout>

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3" >
        </ListView>

    </LinearLayout>

    <!-- Header consisting of Month and Day abbreviations -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#FFFFFFFF"  >

        <TextView
            android:id="@+id/month_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="January"
            android:textSize="20sp"
            android:textStyle="bold"
            android:paddingTop="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

            <TextView
                android:id="@+id/sunday_txt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:text="S" />

            <TextView
                android:id="@+id/monday_txt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:text="M" />

        </LinearLayout>
    </LinearLayout>
</FrameLayout>

ListView是在片段onCreateView

中设置的
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.calendar_list, container, false);

        final ListView listview = (ListView) view.findViewById(R.id.listview1);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
        "Android", "iPhone", "WindowsMobile" };

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }

    final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

动画在clickListener中运行,代码如下:

expand_btn = (ImageButton)view.findViewById(R.id.change_cols_btn);
expand_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

int calendarHeight = calendarView1.getHeight();
final float translateBy = calendarHeight * 0.6f;
ObjectAnimator animator;

if (expanded) {

animator = ObjectAnimator.ofFloat(listView, "translationY", - translateBy);
                        animator.setDuration(300);
                        animator.start();

Logcat收到的错误是:

09-18 16:03:15.506: E/AndroidRuntime(24637): FATAL EXCEPTION: main
09-18 16:03:15.506: E/AndroidRuntime(24637): java.lang.NullPointerException
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:392)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:502)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.ValueAnimator.start(ValueAnimator.java:913)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.ValueAnimator.start(ValueAnimator.java:923)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.animation.ObjectAnimator.start(ObjectAnimator.java:370)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at com.example.eventsmockup.ShowEvents$3.onClick(ShowEvents.java:195)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.view.View.performClick(View.java:4354)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.view.View$PerformClick.run(View.java:17961)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.os.Handler.handleCallback(Handler.java:725)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.os.Looper.loop(Looper.java:137)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at android.app.ActivityThread.main(ActivityThread.java:5328)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at java.lang.reflect.Method.invoke(Method.java:511)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-18 16:03:15.506: E/AndroidRuntime(24637):    at dalvik.system.NativeStart.main(Native Method)

非常感谢您的协助!

1 个答案:

答案 0 :(得分:-3)

这是由代码中的重复引起的。