我正在尝试在 android 片段上实现卡片翻转的动画。这样我就可以让正面有自己的自定义视图,背面也一样。
我的问题是显示卡片背面时,这意味着之前已调用过一次翻转动画。当单击按钮再次显示前面的片段时,它会使应用程序崩溃并出现以下错误。
07-31 20:26:14.649: E/AndroidRuntime(28575): FATAL EXCEPTION: main
07-31 20:26:14.649: E/AndroidRuntime(28575): java.lang.NullPointerException
07-31 20:26:14.649: E/AndroidRuntime(28575): at com.Pivotl.PostcardsFromAlaska.PFANewPostcard_Activity.flipCard(PFANewPostcard_Activity.java:174)
07-31 20:26:14.649: E/AndroidRuntime(28575): at com.Pivotl.PostcardsFromAlaska.PFANewPostcard_Activity.access$0(PFANewPostcard_Activity.java:168)
07-31 20:26:14.649: E/AndroidRuntime(28575): at com.Pivotl.PostcardsFromAlaska.PFANewPostcard_Activity$2.onClick(PFANewPostcard_Activity.java:99)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.view.View.performClick(View.java:4223)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.view.View$PerformClick.run(View.java:17281)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.os.Handler.handleCallback(Handler.java:615)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.os.Looper.loop(Looper.java:137)
07-31 20:26:14.649: E/AndroidRuntime(28575): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 20:26:14.649: E/AndroidRuntime(28575): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 20:26:14.649: E/AndroidRuntime(28575): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 20:26:14.649: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 20:26:14.649: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 20:26:14.649: E/AndroidRuntime(28575): at dalvik.system.NativeStart.main(Native Method)
我的动画是根据本教程制作的:http: //developer.android.com/training/animation/cardflip.html
然后我有三种方法
- 目录卡片()
- 翻转前()
- 翻转()
目录卡片():
private void flipCard()
{
Log.d(tag2, "Log after flipCard:" + mShowingBack);
if(mShowingBack)
{
//Check Items and Save variables
if(backMessageEdit.getText().toString() != null){
//save string
backMessage = backMessageEdit.getText().toString();
}
//show front
flipFront();
return;
}
// Flip to back
flipBack();
}
翻转前():
private void flipFront()
{
mShowingBack = false;
getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_left_in, R.animator.card_flip_left_out,
R.animator.card_flip_right_in, R.animator.card_flip_right_out)
.replace(R.id.postcardFrame, new CardFrontFragment())
.addToBackStack(null)
.commit();
}
翻转():
private void flipBack()
{
getFragmentManager()
.beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
.replace(R.id.postcardFrame, new CardBackFragment())
.addToBackStack(null)
.commit();
mShowingBack = true;
}
在按钮上单击我这样调用flipCard:
cardBackButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
//Check what side is showing
//Then perform flip left or right
flipCard();
}
});
看起来很直接,但我似乎无法用手指缠绕它,前几天我把它放下了,现在才回到它。
我非常感谢在这件事上的任何帮助!