早教吧作业答案频道 -->其他-->
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);
代码如下:
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();
看了 android:空指针异常代...的网友还看了以下:
java求解答~~并且解释一下代码第一行是什么意思?显示m、n之间(mb2)第一个数按从小到大的顺 2020-04-08 …
JAVA程序,输入20以内的素数以下是我写的代码importjava.math.*;publicc 2020-04-08 …
计算机时间时为什么要先除后乘,例如t=t/(1000*3600*24)*(1000*3600*24 2020-07-17 …
servlet为什么只显示Thisisclasstext,usingtheGETmethod我的s 2020-07-17 …
java编程题,输入一段英文文章,单词之间都已经用空格分隔,本人想以每5个单词为一行输出,怎么写? 2020-07-17 …
android:空指针异常代码如下:packagecom.test;importjava.io.B 2020-07-17 …
RandomAccessFile不明白的用法seek有代码请教谢谢了?我的代码:importjav 2020-07-22 …
下面这个action,为什么JSONArrayjsonlist=JSONArray.fromObj 2020-07-26 …
朋友们帮下忙说出这段代码每一行的意思把每一行的意思写出来的OK了importjava.awt.*; 2020-07-27 …
JDK6.0的枚举类型怎么定义importjava.lang.Enum;publicclasstes 2020-11-27 …