Question

Now that this problem is over ill address the rest of it. Heres my working code:

package edu.wmich.lab4_jjohns1119;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation;

public class MainFragment extends Fragment {

//Declare widget objects
Button btnAnimate;
ImageView imgTween;
Animation tweenAnimation;

//create view
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//Inflater
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
//Button
     btnAnimate = (Button)rootView.findViewById(R.id.btnAnimate);   
     //Image
     imgTween = (ImageView)rootView.findViewById(R.id.imgTween);
     //Animation Resource
    tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);
//Button listener for animation
btnAnimate.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //animate image
        imgTween.startAnimation(tweenAnimation);
    }

});
//Return view
return rootView;
}
}

it launches, it navigates, but the animation does nothing. my guess is the order of the code is incorrect ad it is displaying the view before an animation. heres my tween1.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http//schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-45"
android:duration="400"
 />
Was it helpful?

Solution 2

Why are you using getView()? Move all you assignments below rootView and do:

btnAnimate = (Button)rootView.findViewById(R.id.btnAnimate);

For animation to work, change the namespace declaration to:

http://schemas.android.com/apk/res/android

(Note the colon after http is missing in your sample)

....

OTHER TIPS

Your problem lies with assigning your tweenAnimation field

Animation tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);

getActivity() is null at this point in your fragments lifecycle (creation) and will cause loadAnimation to fail or return a null animation. Accessing this will then result in a NPE (if loadAnimation hasn't already thrown one)

Move assigning tweenAnimation to a point in the fragment's lifecycle where the activity will exist like onCreate

ex.

Animation tweenAnimation;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreat(savedInstanceState);
    tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);
}

The problem is that you can not call getView() or getActivity() before the activity is finished building. Therefore you need to only call these methods afterwards by overiding the onActivityCreated(...) method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top