我是 android 新手,我想从服务器获取图像并将它们加载到布局中。但我的代码只运行到Log.e("error","115"); the imageView can't initialize.
这是我的代码:
package com.example.mywaterfallversion2;
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 = 0;
private byte[][] picBytes = new byte[NUM_OF_PIC][];
private int numOfPic = 0;
private int index = 0;
public static int requestTime = 0;
private PictureArrayOfBytes picArray ;
View view;
ImageView imageView;
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;
default :
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");
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 = ((ScrollView) v).getChildAt(0);
if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) {
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","110");
Bitmap bitmap = BitmapFactory.decodeByteArray(picBytes[i], 0, picBytes[i].length);
Log.e("error","113");
Bitmap bitmap2 = BitmapZoom.bitmapZoomByWidth(bitmap, linearlayoutWidth); //compressed the image
Log.e("error","linearlayoutWidth = "+ String.valueOf(linearlayoutWidth));
**Log.e("error","115");**
imageView = new ImageView(this);
Log.e("error","117");
imageView.setImageBitmap(bitmap2);
Log.e("error","105");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(bitmap2.getWidth() + 5, bitmap2.getHeight() + 5);
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();
}
}
}
日志信息:
06-08 02:11:02.109: E/error(900): linearlayoutWidth = 240
06-08 02:11:02.109: E/error(900): 115 (*the code run here,can't continue* )
06-08 02:11:02.109: W/System.err(900): java.lang.NullPointerException
06-08 02:11:02.130: W/System.err(900): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
06-08 02:11:02.130: W/System.err(900): at android.view.View.<init>(View.java:2696)
06-08 02:11:02.130: W/System.err(900): at android.widget.ImageView.<init>(ImageView.java:104)
06-08 02:11:02.139: W/System.err(900): at com.example.mywaterfallversion2.MainActivity.addBitmaps(MainActivity.java:117)
06-08 02:11:02.139: W/System.err(900): at com.example.mywaterfallversion2.MainActivity.access$0(MainActivity.java:105)
06-08 02:11:02.139: W/System.err(900): at com.example.mywaterfallversion2.MainActivity$1.handleMessage(MainActivity.java:41)
06-08 02:11:02.163: W/System.err(900): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 02:11:02.163: W/System.err(900): at android.os.Looper.loop(Looper.java:137)