我尝试动画片段的内容(textViews等),这些片段附加到ViewPager,当我们从片段移动到片段时,动画效果很好。我还需要动画片段,当应用程序被发现并出现问题时可以看到,这里是我的方法代码与支持片段相关
private void initializeFrags() {
List<Fragment> ourlist = new Vector<Fragment>();
ourlist.add(Fragment.instantiate(this, FirstFrag.class.getName()));
ourlist.add(Fragment.instantiate(this, SecondFrag.class.getName()));
ourlist.add(Fragment.instantiate(this, ThirdFrag.class.getName()));
adapter = new ouradapter(super.getSupportFragmentManager(), ourlist);
ourPlace = (ViewPager) super.findViewById(R.id.mypager);
ourPlace.setAdapter(adapter);
// this method generates the errrors
firstAnimation();
ourPlace.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if(arg0==0)
{
View v = adapter.getItem(arg0).getView();
TextView text = (TextView) v.findViewById(R.id.firstText);
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade);
text.startAnimation(fadeInAnimation );
}
Toast.makeText(getApplicationContext(), Integer.toString(arg0), Toast.LENGTH_LONG).show();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
我尝试在此处添加方法firstanimation
,其中firstfragment
的内容为动画,initializeFrags()
在onCreate
期间发布,因此仅在应用启动时执行
以下是firstanimation
方法的代码:
private void firstAnimation()
{
View v = adapter.getItem(0).getView();
TextView text = (TextView) v.findViewById(R.id.firstText);
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade);
text.startAnimation(fadeInAnimation );
}
我收到了错误,
12-08 21:45:33.102: E/Trace(29275): error opening trace file: No such file or directory (2)
12-08 21:45:33.333: E/AndroidRuntime(29275): FATAL EXCEPTION: main
12-08 21:45:33.333: E/AndroidRuntime(29275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.worry.fragmentwithanimation/com.worry.fragmentwithanimation.MainActivity}: java.lang.NullPointerException
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread.access$600(ActivityThread.java:133)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.os.Looper.loop(Looper.java:137)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread.main(ActivityThread.java:4803)
12-08 21:45:33.333: E/AndroidRuntime(29275): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 21:45:33.333: E/AndroidRuntime(29275): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 21:45:33.333: E/AndroidRuntime(29275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-08 21:45:33.333: E/AndroidRuntime(29275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-08 21:45:33.333: E/AndroidRuntime(29275): at dalvik.system.NativeStart.main(Native Method)
12-08 21:45:33.333: E/AndroidRuntime(29275): Caused by: java.lang.NullPointerException
12-08 21:45:33.333: E/AndroidRuntime(29275): at com.worry.fragmentwithanimation.MainActivity.firstAnimation(MainActivity.java:88)
12-08 21:45:33.333: E/AndroidRuntime(29275): at com.worry.fragmentwithanimation.MainActivity.initializeFrags(MainActivity.java:51)
12-08 21:45:33.333: E/AndroidRuntime(29275): at com.worry.fragmentwithanimation.MainActivity.onCreate(MainActivity.java:34)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.Activity.performCreate(Activity.java:5008)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-08 21:45:33.333: E/AndroidRuntime(29275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
12-08 21:45:33.333: E/AndroidRuntime(29275): ... 11 more
我也尝试在片段中制作动画:
TextView first;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first, container,false);
first=(TextView) view.findViewById(R.id.firstText);
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
// TODO Auto-generated method stub
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser)
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade);
first.startAnimation(fadeInAnimation );
}
}
我收到了这个错误
12-08 23:05:38.541: E/AndroidRuntime(31143): FATAL EXCEPTION: main
12-08 23:05:38.541: E/AndroidRuntime(31143): java.lang.NullPointerException
12-08 23:05:38.541: E/AndroidRuntime(31143): at com.worry.fragmentwithanimation.FirstFrag.setUserVisibleHint(FirstFrag.java:34)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.View.measure(View.java:15401)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:622)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:404)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.View.measure(View.java:15401)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4950)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.View.measure(View.java:15401)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.LinearLayout.measureVertical(LinearLayout.java:855)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.View.measure(View.java:15401)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4950)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
12-08 23:05:38.541: E/AndroidRuntime(31143): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.View.measure(View.java:15401)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1856)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4294)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.os.Handler.handleCallback(Handler.java:643)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.os.Looper.loop(Looper.java:137)
12-08 23:05:38.541: E/AndroidRuntime(31143): at android.app.ActivityThread.main(ActivityThread.java:4803)
12-08 23:05:38.541: E/AndroidRuntime(31143): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 23:05:38.541: E/AndroidRuntime(31143): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 23:05:38.541: E/AndroidRuntime(31143): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-08 23:05:38.541: E/AndroidRuntime(31143): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-08 23:05:38.541: E/AndroidRuntime(31143): at dalvik.system.NativeStart.main(Native Method)
我问我怎样才能解决这个问题,以便为活动开始时显示的片段设置动画。我用Google搜索了一段时间,但在其他问题上,当我们将一个片段更改为其他片段时,我只能看到动画,而这个问题由onPageSelected处理。
答案 0 :(得分:0)
你在片段动画尝试中做错了:
TextView first;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first, container,false);
first=(TextView) view.findById(R.id.firstText);
return view;
}
另一次尝试没有机会正常工作,因为在创建活动时可能没有(实际上没有)创建片段。它将在稍后发生,因此当您尝试为其设置动画时,首先为null。