我正在尝试实现卡片在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();
}
flipFront():
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();
}
flipBack():
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();
}
});
看起来非常直接,但我似乎无法用手指环绕它,前几天我把它放下来,现在又回到了它。
我非常感谢有关此事的任何帮助!
答案 0 :(得分:1)
我需要查看更多代码,但我不得不说你没有正确引用backMessageEdit
。