我已经实施了ViewFlipper
,当我不使用清单中的<uses-sdk>
行时,它运行良好。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.warde.loyal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在setContentView
中生成异常。查看日志记录
02-17 10:23:59.479: E/AndroidRuntime(3851): FATAL EXCEPTION: main
02-17 10:23:59.479: E/AndroidRuntime(3851): Process: com.warde.loyal, PID: 3851
02-17 10:23:59.479: E/AndroidRuntime(3851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.warde.loyal/com.warde.loyal.MainActivity}: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.os.Looper.loop(Looper.java:157)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread.main(ActivityThread.java:5335)
02-17 10:23:59.479: E/AndroidRuntime(3851): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 10:23:59.479: E/AndroidRuntime(3851): at java.lang.reflect.Method.invoke(Method.java:515)
02-17 10:23:59.479: E/AndroidRuntime(3851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-17 10:23:59.479: E/AndroidRuntime(3851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-17 10:23:59.479: E/AndroidRuntime(3851): at dalvik.system.NativeStart.main(Native Method)
02-17 10:23:59.479: E/AndroidRuntime(3851): Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
02-17 10:23:59.479: E/AndroidRuntime(3851): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
02-17 10:23:59.479: E/AndroidRuntime(3851): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:340)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.Activity.setContentView(Activity.java:1973)
02-17 10:23:59.479: E/AndroidRuntime(3851): at com.warde.loyal.MainActivity.onCreate(MainActivity.java:32)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.Activity.performCreate(Activity.java:5389)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
02-17 10:23:59.479: E/AndroidRuntime(3851): ... 11 more
02-17 10:23:59.479: E/AndroidRuntime(3851): Caused by: java.lang.reflect.InvocationTargetException
02-17 10:23:59.479: E/AndroidRuntime(3851): at java.lang.reflect.Constructor.constructNative(Native Method)
02-17 10:23:59.479: E/AndroidRuntime(3851): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
02-17 10:23:59.479: E/AndroidRuntime(3851): ... 26 more
02-17 10:23:59.479: E/AndroidRuntime(3851): Caused by: java.lang.OutOfMemoryError
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.content.res.Resources.loadDrawable(Resources.java:3054)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.widget.ImageView.<init>(ImageView.java:133)
02-17 10:23:59.479: E/AndroidRuntime(3851): at android.widget.ImageView.<init>(ImageView.java:123)
02-17 10:23:59.479: E/AndroidRuntime(3851): ... 29 more
这是我实施ViewFlipper
的代码。
public class MainActivity extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper;
private AnimationListener mAnimationListener;
private Context mContext;
@SuppressWarnings("deprecation")
private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
mViewFlipper.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(4000);
mViewFlipper.startFlipping();
//animation listener
mAnimationListener = new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
//animation started event
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
//TODO animation stopped event
}
};
}
class SwipeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showNext();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showPrevious();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
我想,也许我需要实施AsynkTask
?
代码中应该更改什么。 ?
提前致谢...
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/lightning" />
<TextView
style="@style/ImageTitle"
android:text="@string/lightning" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/color_baloons" />
<TextView
style="@style/ImageTitle"
android:text="@string/color_baloons" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/natural_wall" />
<TextView
style="@style/ImageTitle"
android:text="@string/natural_wall" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
答案 0 :(得分:0)