我正在尝试用 5 张图片做一个 HorizontalScrollView。我像这样实现了xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spacing="40dp"
android:scrollbars="horizontal"
android:fadeScrollbars="false"
android:layout_marginBottom="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"/>
</HorizontalScrollView>
代码是这样的
package com.infobest.praiser.activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.infobest.praiser.R;
import com.infobest.praiser.actionbar.ActionBarActivity;
public class TutorialActivity extends ActionBarActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);
Integer[] mImageIds = {R.drawable.tutorial1, R.drawable.tutorial2, R.drawable.tutorial3,
R.drawable.tutorial4, R.drawable.tutorial5};
HorizontalScrollView h = (HorizontalScrollView) this.findViewById(R.id.gallery);
LinearLayout topLinearLayout = new LinearLayout(this);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 5; i++)
{
final ImageView imageView = new ImageView(this);
imageView.setImageResource(mImageIds[i]);
topLinearLayout.addView(imageView);
}
h.addView(topLinearLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_tutorial, menu);
// Calling super after populating the menu is necessary here to ensure
// that the
// action bar helpers have a chance to handle this event.
return super.onCreateOptionsMenu(menu);
}
}
我到底做错了什么,因为我得到了 OutOfMemoryError
日志猫
FATAL EXCEPTION: main java.lang.OutOfMemoryError at
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
at android.content.res.Resources.loadDrawable(Resources.java:1937)
at android.content.res.Resources.getDrawable(Resources.java:664) at
android.widget.ImageView.resolveUri(ImageView.java:542) at
android.widget.ImageView.setImageResource(ImageView.java:315) at
com.infobest.praiser.activity.TutorialActivity.onCreate(TutorialActivity.java:30)
at android.app.Activity.performCreate(Activity.java:4465) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:4340) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at
dalvik.system.NativeStart.main(Native Method)