2 b look b_look 于 2013.06.07 23:24 提问

无法执行 ImageView imageView = new ImageView(this);

我写的瀑布流显示图片

每次执行到Log.e("error","101成功压缩图片")这句就没办法执行了,下面是我的代码

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class MainActivity extends Activity implements Runnable{

private final int NUM_OF_PIC = 30;  //每次请求获取的图片数量
private  LinearLayout linearLayout1 = null;
private  LinearLayout linearLayout2 = null;
private  LinearLayout linearLayout3 = null;
private  View scrollView = null;
public static int USE_LINEAR_INTERVAL = 0;
public static int linearlayoutWidth;
private byte[][] picBytes = new byte[NUM_OF_PIC][];
private int numOfPic = 0;//已加载的图片数量
private int index = 0; 
public static int requestTime = 0;
private PictureArrayOfBytes picArray ;

private Handler mHandler = new Handler(){  
    public void handleMessage(Message msg) {  
        switch (msg.what) {  
        case 1:   
          try {
            Log.e("error","39");
            addBitmaps();
            Log.e("error","41");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            break;  
        }  
    };  
};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    linearLayout1 = (LinearLayout)findViewById(R.id.main_linearlayout1);
    linearLayout2 = (LinearLayout)findViewById(R.id.main_linearlayout2);
    linearLayout3 = (LinearLayout)findViewById(R.id.main_linearlayout3);
    linearlayoutWidth =  (int)(getWindowManager().getDefaultDisplay().getWidth()/3);
    Log.e("error","creat: " + String.valueOf(linearlayoutWidth));

    scrollView = findViewById(R.id.scroll);
    Log.e("error","61");
    final Thread thread = new Thread(new MainActivity());
    Log.e("error","65");
    thread.start();
    Log.e("error","67");
    Log.e("info", "context: "+(context == null));
    scrollView.setOnTouchListener(new OnTouchListener() {  
        @Override  
        public boolean onTouch(View v, MotionEvent event) {  
            switch (event.getAction()) {  
                case MotionEvent.ACTION_DOWN :  
                    break;  
                case MotionEvent.ACTION_MOVE :  
                    index++;  
                    break;  
                default :  
                    break;  
            }  
            if (event.getAction() == MotionEvent.ACTION_UP &&  index > 0) {  
                index = 0;  
                View view = ((ScrollView) v).getChildAt(0);  
                if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) {  
                    //加载数据代码  
                    //每次加载30张图片,一共加载5次
                        if (numOfPic >= NUM_OF_PIC){
                            numOfPic = 0;
                            thread.start();
                        }
                }  
            }  
            return false;  
        }  
    });  
    Log.e("error","90");
}



private void addBitmaps() throws Exception{
     Log.e("error","87:开始加载图片");


    for (int i = 0; i < NUM_OF_PIC; i++){
        Log.e("error","将图片数据转换成bitmap");

        Bitmap bitmap = BitmapFactory.decodeByteArray(picBytes[i], 0, picBytes[i].length);
        Log.e("error","改编图片的大小");
        Bitmap bitmap2 = BitmapZoom.bitmapZoomByWidth(bitmap, 240); //压缩图片大小
        Log.e("error","101成功压缩图片");
        Log.e("info", "context: "+(context == null));
        ImageView imageView = new ImageView(this);
        Log.e("error","103");
        imageView.setImageBitmap(bitmap);
        Log.e("error","105");
        //根据图片的大小,为图片挖坑
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(bitmap2.getWidth(), bitmap2.getHeight());
        //将坑和图片对象联系起来
        Log.e("error","109");
        imageView.setLayoutParams(layoutParams);
        Log.e("error","111");
        //按照顺序加载图片
        switch (USE_LINEAR_INTERVAL) 
        {
            case 0:
                linearLayout1.addView(imageView);
                numOfPic++;
                break;
            case 1:
                linearLayout2.addView(imageView);
                numOfPic++;
                break;
            case 2:
                linearLayout3.addView(imageView);
                numOfPic++;
                break;
            default:
                break;
        }
        USE_LINEAR_INTERVAL++;
        USE_LINEAR_INTERVAL= USE_LINEAR_INTERVAL%3;
    }
}
@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        requestTime++;
        picArray = new PictureArrayOfBytes(requestTime);
        picBytes = picArray.getPicByteArray();
        Message message = new Message();  
        message.what = 1;  
        Log.e("error","153");
        mHandler.sendMessage(message);  
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

下面是我的logcat中的信息:

06-07 15:08:39.146: D/dalvikvm(2418): GC_CONCURRENT freed 292K, 4% free 13039K/13511K, paused 6ms+6ms
06-07 15:08:46.238: E/error(2418): 153
06-07 15:08:46.238: E/error(2418): 39
06-07 15:08:46.238: E/error(2418): 87:开始加载图片
06-07 15:08:46.238: E/error(2418): 将图片数据转换成bitmap
06-07 15:08:46.315: D/dalvikvm(2418): GC_FOR_ALLOC freed 252K, 4% free 13084K/13575K, paused 76ms
06-07 15:08:46.315: I/dalvikvm-heap(2418): Grow heap (frag case) to 12.987MB for 160016-byte allocation
06-07 15:08:46.498: D/dalvikvm(2418): GC_FOR_ALLOC freed 06-07 15:08:46.498: E/error(2418): 改编图片的大小
06-07 15:08:46.566: D/dalvikvm(2418): GC_FOR_ALLOC freed 27K, 5% free 13214K/13767K, paused 66ms
06-07 15:08:46.575: I/dalvikvm-heap(2418): Grow heap (frag case) to 13.181MB for 230416-byte allocation
06-07 15:08:46.635: D/dalvikvm(2418): GC_FOR_ALLOC freed 0K, 5% free 13439K/14023K, paused 60ms
**06-07 15:08:46.645: E/error(2418): 101成功压缩图片
06-07 15:08:46.655: W/System.err(2418): java.lang.NullPointerException**
06-07 15:08:46.685: W/System.err(2418):at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
06-07 15:08:46.685: W/System.err(2418): at android.view.View.(View.java:2696)
06-07 15:08:46.685: W/System.err(2418): at android.widget.ImageView.(ImageView.java:104)
06-07 15:08:46.685: W/System.err(2418): at com.example.mywaterfallversion2.MainActivity.addBitmaps(MainActivity.java:113)
06-07 15:08:46.685: W/System.err(2418): at com.example.mywaterfallversion2.MainActivity.access$0(MainActivity.java:101)
06-07 15:08:46.698: W/System.err(2418): at com.example.mywaterfallversion2.MainActivity$1.handleMessage(MainActivity.java:39)
06-07 15:08:46.698: W/System.err(2418): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 15:08:46.698: W/System.err(2418): at android.os.Looper.loop(Looper.java:137)
06-07 15:08:46.698: W/System.err(2418): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-07 15:08:46.698: W/System.err(2418): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 15:08:46.706: W/System.err(2418): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 15:08:46.706: W/System.err(2418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 15:08:46.706: W/System.err(2418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 15:08:46.706: W/System.err(2418): at dalvik.system.NativeStart.main(Native Method)

2个回答

Jave_
Jave_   2013.06.08 09:07

你的context申明在哪里???

shaojie519
shaojie519   2013.06.08 13:31

你的Context必须是一个Activity类型的Context引用.
换成ImageView imageView = new ImageView(MainActivitty.this);

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!