java.lang.nullpointerexception应用程序在DrawableAnimation上崩溃

时间:2014-12-04 22:01:58

标签: android animationdrawable

ScreenSlide活动代码:

package com.example.basicgrid;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class ScreenSlideActivity extends FragmentActivity {
/**
 * The number of pages (wizard steps) to show in this demo.
 */
private static final int NUM_PAGES = 4;

/**
 * The pager widget, which handles animation and allows swiping horizontally to access previous
 * and next wizard steps.
 */
private ViewPager mPager;

/**
 * The pager adapter, which provides the pages to the view pager widget.
 */
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
}

@Override
public void onBackPressed() {
    if (mPager.getCurrentItem() == 0) {
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.
        super.onBackPressed();
    } else {
        // Otherwise, select the previous step.
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}

/**
 * A simple pager adapter that represents 4 ScreenSlidePageFragment objects, in
 * sequence.
 */

 private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
     public ScreenSlidePagerAdapter(FragmentManager fm) {
         super(fm);
     }

     public void setCurrentItem (int item, boolean smoothScroll) {
         mPager.setCurrentItem(item, smoothScroll);
     }

     @Override
     public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new screenview1();
            case 1:
                return new screenview2();
            case 2:
                return new screenview3();
            case 3:
           //   return new screenview4();    
            }
            return null;
        }

     @Override
     public int getCount() {
         return NUM_PAGES;
     }
 }

我在src文件夹结构下有4个单独的文件名为screenview 1/2/3 / 4.java

ScreenView1.java代码:

package com.example.basicgrid;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class screenview1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.tutorial, container, false);

    final ImageView animImageView = (ImageView) rootView.findViewById(R.id.ivAnimation);
    final TextView tv = (TextView)rootView.findViewById(R.id.text_vp);
    animImageView.setBackgroundResource(R.drawable.anim);
    tv.setText("The game has 9 small Tic Tac Toe-s which are played simultaneously.");
    animImageView.post(new Runnable() {
        @Override
        public void run() {
            AnimationDrawable frameAnimation =
                (AnimationDrawable) animImageView.getBackground();
            frameAnimation.start();
        }
    });
    return rootView;
    }
}`

在@drawable文件夹中,我有一个名为anim.xml,anim2.xml的文件,以及每个screenview的单独文件。 每个动画文件都有.png文件,这些文件作为动画运行。 anim1,anim2工作得很好。

我的应用程序使用以下logcat在anim3.xml上崩溃:

12-05 03:23:10.598:D / libEGL(4012):已加载/system/lib/egl/libEGL_mali.so

12-05 03:23:10.608:D / libEGL(4012):已加载/system/lib/egl/libGLESv1_CM_mali.so

12-05 03:23:10.618:D / libEGL(4012):已加载/system/lib/egl/libGLESv2_mali.so

12-05 03:23:10.668:D / OpenGLRenderer(4012):启用调试模式0

12-05 03:23:12.730:D / dalvikvm(4012):GC_FOR_ALLOC释放2481K,18%免费13574K / 16387K,暂停39ms,总计55ms

12-05 03:23:12.840:D / dalvikvm(4012):GC_FOR_ALLOC释放4K,9%免费15070K / 16387K,暂停49ms,总计58ms

12-05 03:23:12.870:I / dalvikvm-heap(4012):将堆(frag case)增长到25.380MB以进行3456016字节分配

12-05 03:23:13.040:D / dalvikvm(4012):GC_CONCURRENT释放4K,7%免费18477K / 19783K,暂停30ms + 3ms,总计170ms

12-05 03:23:24.552:D / dalvikvm(4012):GC_FOR_ALLOC释放1561K,14%免费17903K / 20679K,暂停22ms,总计24ms

12-05 03:23:24.562:I / dalvikvm-heap(4012):将堆(frag case)增长到26.731MB,用于1971232字节分配

12-05 03:23:24.632:D / dalvikvm(4012):GC_CONCURRENT释放4279K,23%免费19196K / 24647K,暂停13ms + 3ms,总计38ms

12-05 03:23:24.682:D / dalvikvm(4012):GC_FOR_ALLOC释放1712K,18%免费20265K / 24647K,暂停15ms,总计16ms

12-05 03:23:24.692:I / dalvikvm-heap(4012):将堆(frag case)增长到29.038MB,用于1971232字节分配

12-05 03:23:24.752:D / dalvikvm(4012):GC_CONCURRENT释放856K,13%免费24972K / 28615K,暂停12ms + 3ms,总计35ms

12-05 03:23:24.802:D / dalvikvm(4012):GC_FOR_ALLOC释放1711K,15%免费26042K / 30599K,暂停17ms,总计17ms

12-05 03:23:24.802:I / dalvikvm-heap(4012):将堆(frag case)增长到34.679MB,用于1971232字节分配

12-05 03:23:27.665:D / dalvikvm(4012):GC_FOR_ALLOC释放1731K,14%免费29917K / 34567K,暂停21ms,总计21ms

12-05 03:23:27.725:D / dalvikvm(4012):GC_FOR_ALLOC释放857K,13%免费31841K / 36551K,暂停18ms,总计18ms

12-05 03:23:27.775:D / dalvikvm(4012):GC_FOR_ALLOC释放855K,13%免费33767K / 38535K,暂停17ms,总计17ms

12-05 03:23:27.825:D / dalvikvm(4012):GC_FOR_ALLOC释放855K,12%免费35693K / 40519K,暂停20ms,总计20ms

12-05 03:23:29.917:D / AndroidRuntime(4012):关闭虚拟机

12-05 03:23:29.917:W / dalvikvm(4012):threadid = 1:线程退出未捕获的异常 (组= 0x414d9318)

12-05 03:23:29.937:E / AndroidRuntime(4012):致命异常:主

12-05 03:23:29.937:E / AndroidRuntime(4012):java.lang.NullPointerException

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.support.v4.view.ViewPager.populate(ViewPager.java:1053)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.support.v4.view.ViewPager.populate(ViewPager.java:919)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:249)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.view.Choreographer.doCallbacks(Choreographer.java:555)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.view.Choreographer.doFrame(Choreographer.java:524)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.os.Handler.handleCallback(Handler.java:615)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.os.Handler.dispatchMessage(Handler.java:92)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.os.Looper.loop(Looper.java:137)

12-05 03:23:29.937:E / AndroidRuntime(4012):在android.app.ActivityThread.main(ActivityThread.java:4744)

12-05 03:23:29.937:E / AndroidRuntime(4012):at java.lang.reflect.Method.invokeNative(Native Method)

12-05 03:23:29.937:E / AndroidRuntime(4012):at java.lang.reflect.Method.invoke(Method.java:511)

12-05 03:23:29.937:E / AndroidRuntime(4012):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)

12-05 03:23:29.937:E / AndroidRuntime(4012):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

12-05 03:23:29.937:E / AndroidRuntime(4012):at dalvik.system.NativeStart.main(Native Method)

有人请解释我在这里做错了什么......

编辑:

anim3.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    
    <item android:drawable="@drawable/d0" android:duration="1000" />
    <item android:drawable="@drawable/d1" android:duration="100" />
    <item android:drawable="@drawable/d2" android:duration="900" />
    <item android:drawable="@drawable/d3" android:duration="1000" />
</animation-list>

tutorial.xml(在screenview1.java中调用:

<?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"
    android:background="#000000"
    android:scaleType="fitXY"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/ivAnimation"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:layout_above="@+id/text_vp"
    android:layout_centerHorizontal="true"
    android:contentDescription="Animation" />

<TextView
    android:id="@+id/text_vp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ivAnimation"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="62dp"
    android:gravity="center"
    android:paddingBottom="10sp"
    android:paddingLeft="10sp"
    android:paddingRight="10sp"
    android:paddingTop="10sp"
    android:textAlignment="center"
    android:textColor="#FFFFFF"
    android:textSize="20sp" />
</RelativeLayout>

0 个答案:

没有答案