我似乎无法通过Android开发网站上的Card Flip教程。我跟着并修改它以满足我的需求,但它崩溃了,我不知道为什么。 Eclipse在我的java代码中没有给我任何错误。想知道是否有人有任何建议我可以尝试。我已经包含了java和logcat。谢谢你的时间。
爪哇:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
/**
* Whether or not we're showing the back of the card (otherwise showing the front).
*/
private boolean mShowingBack = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, new CardFrontFragment())
.commit();
}else {
mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);
}
/**
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
*/
}
private void flipCard() {
if (mShowingBack) {
getFragmentManager().popBackStack();
return;
}
// Flip to the back.
mShowingBack = true;
// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
getSupportFragmentManager()
.beginTransaction()
// Replace the default fragment animations with animator resources representing
// rotations when switching to the back of the card, as well as animator
// resources representing rotations when flipping back to the front (e.g. when
// the system Back button is pressed).
.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 any fragments currently in the container view with a fragment
// representing the next page (indicated by the just-incremented currentPage
// variable).
.replace(R.id.container, new CardBackFragment())
// Add this transaction to the back stack, allowing users to press Back
// to get to the front of the card.
.addToBackStack(null)
// Commit the transaction.
.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.card_flip) {
flipCard();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
/**
* A fragment representing the front of the card.
*/
public static class CardFrontFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.card_front, container, false);
}
}
/**
* A fragment representing the back of the card.
*/
public static class CardBackFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.card_back, container, false);
}
}
}
logcat的:
04-09 01:44:56.440: D/dalvikvm(799): Not late-enabling CheckJNI (already on)
04-09 01:45:01.850: D/dalvikvm(799): GC_FOR_ALLOC freed 87K, 8% free 2905K/3124K, paused 37ms, total 41ms
04-09 01:45:01.850: I/dalvikvm-heap(799): Grow heap (frag case) to 8.892MB for 6220816-byte allocation
04-09 01:45:01.900: D/dalvikvm(799): GC_FOR_ALLOC freed 3K, 3% free 8976K/9200K, paused 43ms, total 43ms
04-09 01:45:02.530: D/(799): HostConnection::get() New Host Connection established 0xb89a8468, tid 799
04-09 01:45:02.660: W/EGL_emulation(799): eglSurfaceAttrib not implemented
04-09 01:45:02.670: D/OpenGLRenderer(799): Enabling debug mode 0
04-09 01:45:06.140: W/EGL_emulation(799): eglSurfaceAttrib not implemented
04-09 01:45:10.960: W/EGL_emulation(799): eglSurfaceAttrib not implemented
04-09 01:45:13.390: W/EGL_emulation(799): eglSurfaceAttrib not implemented
04-09 01:46:59.588: D/dalvikvm(1088): GC_FOR_ALLOC freed 84K, 7% free 2905K/3120K, paused 36ms, total 38ms
04-09 01:46:59.588: I/dalvikvm-heap(1088): Grow heap (frag case) to 8.892MB for 6220816-byte allocation
04-09 01:46:59.638: D/dalvikvm(1088): GC_FOR_ALLOC freed 3K, 3% free 8976K/9196K, paused 40ms, total 40ms
04-09 01:47:00.298: D/(1088): HostConnection::get() New Host Connection established 0xb8acdf90, tid 1088
04-09 01:47:00.458: W/EGL_emulation(1088): eglSurfaceAttrib not implemented
04-09 01:47:00.478: D/OpenGLRenderer(1088): Enabling debug mode 0
04-09 01:47:03.968: W/EGL_emulation(1088): eglSurfaceAttrib not implemented
04-09 01:47:08.418: W/EGL_emulation(1088): eglSurfaceAttrib not implemented
04-09 01:48:39.118: D/dalvikvm(1145): GC_FOR_ALLOC freed 87K, 8% free 2905K/3124K, paused 41ms, total 45ms
04-09 01:48:39.118: I/dalvikvm-heap(1145): Grow heap (frag case) to 8.892MB for 6220816-byte allocation
04-09 01:48:39.158: D/dalvikvm(1145): GC_FOR_ALLOC freed 3K, 3% free 8976K/9200K, paused 31ms, total 32ms
04-09 01:48:39.728: D/(1145): HostConnection::get() New Host Connection established 0xb8acdfa0, tid 1145
04-09 01:48:39.858: W/EGL_emulation(1145): eglSurfaceAttrib not implemented
04-09 01:48:39.888: D/OpenGLRenderer(1145): Enabling debug mode 0
04-09 01:48:45.628: W/EGL_emulation(1145): eglSurfaceAttrib not implemented
04-09 01:48:46.758: D/AndroidRuntime(1145): Shutting down VM
04-09 01:48:46.758: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb3a87ba8)
04-09 01:48:46.778: E/AndroidRuntime(1145): FATAL EXCEPTION: main
04-09 01:48:46.778: E/AndroidRuntime(1145): Process: you.ca.mohawk.lab6b, PID: 1145
04-09 01:48:46.778: E/AndroidRuntime(1145): java.lang.RuntimeException: Unknown animation name: objectAnimator
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:124)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:114)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:91)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:72)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.FragmentManagerImpl.loadAnimation(FragmentManager.java:776)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1011)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1212)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:639)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.os.Handler.handleCallback(Handler.java:733)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:95)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
04-09 01:48:46.778: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-09 01:48:46.778: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 01:48:46.778: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 01:48:46.778: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-09 01:48:46.778: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-09 01:48:46.778: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您收到了运行时异常,因为您尚未初始化Animation objectAnimator
。
java.lang.RuntimeException: Unknown animation name: objectAnimator
答案 1 :(得分:0)
请检查您的Android版本,因为它仅适用于3.0或更高版本。
答案 2 :(得分:0)
希望现在已经解决了这个问题但是我遇到了同样的问题并且建议对我不起作用。工作(将google's sample card flip code与我的比较后)的工作原理是使用import android.support.v4.app.Fragment;
是我问题的根源。将其更改为import android.app.Fragment;
可以解除我的错误。但是,此更改将影响应用程序适用的版本。