计时器活动出错

时间:2014-01-14 18:03:35

标签: android android-activity timer handler

我的活动计时器改变活动与其他活动在AVD上工作正常但在实际设备中我得到的错误是代码:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
        final Intent mainIntent = new Intent(splash.this,MainActivity.class);
        splash.this.startActivity(mainIntent);
        splash.this.finish();
        }
        }, 5000);

这是logcat输出:

01-14 21:16:39.146: E/AndroidRuntime(10899): FATAL EXCEPTION: main
01-14 21:16:39.146: E/AndroidRuntime(10899): java.lang.OutOfMemoryError
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.content.res.Resources.loadDrawable(Resources.java:2854)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.content.res.Resources.getDrawable(Resources.java:1521)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.ImageView.resolveUri(ImageView.java:628)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.ImageView.setImageResource(ImageView.java:361)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at com.safshari.mandegar.ImageAdapter.getView(ImageAdapter.java:70)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.AbsListView.obtainView(AbsListView.java:2597)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.GridView.makeAndAddView(GridView.java:1344)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.GridView.makeRow(GridView.java:344)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.GridView.fillDown(GridView.java:286)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.GridView.fillFromTop(GridView.java:420)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.GridView.layoutChildren(GridView.java:1232)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.AbsListView.onLayout(AbsListView.java:2431)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.view.View.layout(View.java:14482)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.view.ViewGroup.layout(ViewGroup.java:4469)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.view.View.layout(View.java:14482)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.view.ViewGroup.layout(ViewGroup.java:4469)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
01-14 21:16:39.146: E/AndroidRuntime(10899):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)

如果需要信息,我的设备是Galaxy s4。

我的图像适配器代码在哪里应该输入毕加索代码?

public class ImageAdapter extends BaseAdapter {
private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.pic1,R.drawable.pic2,
        R.drawable.pic3,R.drawable.pic4,
        R.drawable.pic5,R.drawable.pic6,
        R.drawable.pic7,R.drawable.pic8,
        R.drawable.pic9,R.drawable.pic10,
        R.drawable.pic11,R.drawable.pic12,
        R.drawable.pic13,R.drawable.pic14,
        R.drawable.pic15,R.drawable.pic16,
        R.drawable.pic17,R.drawable.pic18,
        R.drawable.pic19,R.drawable.pic20,
        R.drawable.pic21,R.drawable.pic22,
        R.drawable.pic23,
        R.drawable.pic25,R.drawable.pic26,
        R.drawable.pic27,R.drawable.pic28,
        R.drawable.pic29,R.drawable.pic30,
        R.drawable.pic31,R.drawable.pic32,
        R.drawable.pic33,R.drawable.pic34,
        R.drawable.pic35,R.drawable.pic36,
        R.drawable.pic37,R.drawable.pic38,
        R.drawable.pic39,R.drawable.pic40,
        R.drawable.pic41,R.drawable.pic42,
        R.drawable.pic43,R.drawable.pic44,
        R.drawable.pic45,R.drawable.pic46,
        R.drawable.pic47,R.drawable.pic48,
        R.drawable.pic49,R.drawable.pic50,
        R.drawable.pic51,R.drawable.pic52,
        R.drawable.pic53,R.drawable.pic54,
        R.drawable.pic55


};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
    return imageView;
}

}

3 个答案:

答案 0 :(得分:1)

您尝试将大Bitmap加载到View时计时器不是错误。 您无法加载位图,我建议使用maxDimension调整大小: (在这种情况下,它来自网址:input

BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
Bitmap myBitmap = BitmapFactory.decodeStream(input, null, option);

int scale = 1;
if (option.outHeight > maxDimension || option.outWidth > maxDimension) {
    scale = (int) Math.pow(2, (int) Math.round(Math.log(maxDimension / (double) Math.max(option.outHeight, option.outWidth)) / Math.log(0.5)));
}

BitmapFactory.Options option2 = new BitmapFactory.Options();
option2.inSampleSize = scale;
myBitmap = BitmapFactory.decodeStream(input2, null, option2);

答案 1 :(得分:1)

解决问题的最快方法是使用Jake Wharton的Picasso库。它允许在一行代码中加载您的图像并管理缓存,转换,下载等。

以下是下载图书馆的链接: http://square.github.io/picasso/

您需要的所有代码是:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

将其放置在网格适配器中,提供图像URL或可绘制或...并替换图像持有者的imageView。

它应该像魅力一样:)

答案 2 :(得分:0)

由于您要加载的图片太大,因此内存不足。它可能在第二个Activity的布局文件中设置,因此只有在您触发Error时才能获得Intent。这与计时器无关。你能解释一下这种情况并粘贴剩余的堆栈跟踪吗?