ScreenSlide Activity code:
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;
}
}
I have 4 separate files named as screenview 1/2/3/4.java under src folder structure
ScreenView1.java code:
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;
}
}`
In the @drawable folder I have a file named anim.xml, anim2.xml and so on separate files for each screenview. Each anim file has .png files which run as an animation. The anim1,anim2 work perfectly fine.
My app crashes on the anim3.xml with the following logcat:
12-05 03:23:10.598: D/libEGL(4012): loaded /system/lib/egl/libEGL_mali.so
12-05 03:23:10.608: D/libEGL(4012): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-05 03:23:10.618: D/libEGL(4012): loaded /system/lib/egl/libGLESv2_mali.so
12-05 03:23:10.668: D/OpenGLRenderer(4012): Enabling debug mode 0
12-05 03:23:12.730: D/dalvikvm(4012): GC_FOR_ALLOC freed 2481K, 18% free 13574K/16387K, paused 39ms, total 55ms
12-05 03:23:12.840: D/dalvikvm(4012): GC_FOR_ALLOC freed 4K, 9% free 15070K/16387K, paused 49ms, total 58ms
12-05 03:23:12.870: I/dalvikvm-heap(4012): Grow heap (frag case) to 25.380MB for 3456016-byte allocation
12-05 03:23:13.040: D/dalvikvm(4012): GC_CONCURRENT freed 4K, 7% free 18477K/19783K, paused 30ms+3ms, total 170ms
12-05 03:23:24.552: D/dalvikvm(4012): GC_FOR_ALLOC freed 1561K, 14% free 17903K/20679K, paused 22ms, total 24ms
12-05 03:23:24.562: I/dalvikvm-heap(4012): Grow heap (frag case) to 26.731MB for 1971232-byte allocation
12-05 03:23:24.632: D/dalvikvm(4012): GC_CONCURRENT freed 4279K, 23% free 19196K/24647K, paused 13ms+3ms, total 38ms
12-05 03:23:24.682: D/dalvikvm(4012): GC_FOR_ALLOC freed 1712K, 18% free 20265K/24647K, paused 15ms, total 16ms
12-05 03:23:24.692: I/dalvikvm-heap(4012): Grow heap (frag case) to 29.038MB for 1971232-byte allocation
12-05 03:23:24.752: D/dalvikvm(4012): GC_CONCURRENT freed 856K, 13% free 24972K/28615K, paused 12ms+3ms, total 35ms
12-05 03:23:24.802: D/dalvikvm(4012): GC_FOR_ALLOC freed 1711K, 15% free 26042K/30599K, paused 17ms, total 17ms
12-05 03:23:24.802: I/dalvikvm-heap(4012): Grow heap (frag case) to 34.679MB for 1971232-byte allocation
12-05 03:23:27.665: D/dalvikvm(4012): GC_FOR_ALLOC freed 1731K, 14% free 29917K/34567K, paused 21ms, total 21ms
12-05 03:23:27.725: D/dalvikvm(4012): GC_FOR_ALLOC freed 857K, 13% free 31841K/36551K, paused 18ms, total 18ms
12-05 03:23:27.775: D/dalvikvm(4012): GC_FOR_ALLOC freed 855K, 13% free 33767K/38535K, paused 17ms, total 17ms
12-05 03:23:27.825: D/dalvikvm(4012): GC_FOR_ALLOC freed 855K, 12% free 35693K/40519K, paused 20ms, total 20ms
12-05 03:23:29.917: D/AndroidRuntime(4012): Shutting down VM
12-05 03:23:29.917: W/dalvikvm(4012): threadid=1: thread exiting with uncaught exception (group=0x414d9318)
12-05 03:23:29.937: E/AndroidRuntime(4012): FATAL EXCEPTION: main
12-05 03:23:29.937: E/AndroidRuntime(4012): java.lang.NullPointerException
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.view.Choreographer.doFrame(Choreographer.java:524)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.os.Handler.handleCallback(Handler.java:615)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 03:23:29.937: E/AndroidRuntime(4012): at android.os.Looper.loop(Looper.java:137)
12-05 03:23:29.937: E/AndroidRuntime(4012): at 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)
Someone please explain what am i doing wrong here...
Edit:
anim3.xml code:
<?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 (called in 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>