早教吧 育儿知识 作业答案 考试题库 百科 知识分享

android:空指针异常代码如下:packagecom.test;importjava.io.ByteArrayOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URI;importjava

题目详情
android:空指针异常
代码如下:
package com.test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
\x05private Button but;
\x05private ImageView img;
\x05private EditText et;
\x05private readImg myReadImg;
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but=(Button)findViewById(R.id.but);
et=(EditText)findViewById(R.id.et);
img=(ImageView)findViewById(R.id.img);
but.setOnClickListener(new OnClickListener() {
\x05\x05\x05
\x05\x05\x05@Override
\x05\x05\x05public void onClick(View v) {
\x05\x05\x05\x05// TODO Auto-generated method stub
\x05\x05\x05\x05String path=et.getText().toString();
\x05\x05\x05\x05try {
\x05\x05\x05\x05\x05byte data[]=myReadImg.read(path);
\x05\x05\x05\x05\x05Bitmap bitmap=BitmapFactory.decodeByteArray(data,0,data.length);
\x05\x05\x05\x05\x05img.setImageBitmap(bitmap);
\x05\x05\x05\x05} catch (Exception e) {
\x05\x05\x05\x05\x05// TODO Auto-generated catch block
\x05\x05\x05\x05\x05e.printStackTrace();
\x05\x05\x05\x05\x05Toast.makeText(MainActivity.this,"加载失败",2000);
\x05\x05\x05\x05}
\x05\x05\x05\x05
\x05\x05\x05\x05
\x05\x05\x05}
\x05\x05});
}
private class readImg
{
\x05private static final String TAG="readImg";
\x05public byte[] read (String path) throws Exception
\x05{
\x05\x05InputStream input;
\x05\x05ByteArrayOutputStream outStream=new ByteArrayOutputStream();
\x05\x05URL url=new URL(path);
\x05\x05HttpURLConnection conn=(HttpURLConnection)url.openConnection();
\x05\x05conn.setConnectTimeout(5000);
\x05\x05conn.setRequestMethod("GET");
\x05\x05if(conn.getResponseCode()==200)
\x05\x05{
\x05\x05\x05input=conn.getInputStream();
\x05\x05\x05byte b[]=new byte[1024];
\x05\x05\x05int len=0;
\x05\x05\x05while((len=input.read(b))!=-1)
\x05\x05\x05{
\x05\x05\x05\x05outStream.write(b,0,len);
\x05\x05\x05\x05Log.i(TAG,"test");
\x05\x05\x05}
\x05\x05\x05return b;
\x05\x05}
\x05\x05\x05return null;
\x05\x05
\x05\x05
\x05\x05\x05
\x05\x05
\x05}
}
}
//错误行:
byte data[]=myReadImg.read(path);
▼优质解答
答案和解析
private readImg myReadImg = new myReadImg();