求助:AsyncTask中无法调用mainActivity中的变量?

Umbra_sccc 2019-03-22 11:42:50
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class SearchActivity extends AppCompatActivity {
public Bitmap bm = null;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
TextView textView1 = (TextView) findViewById(R.id.result_text_1);
ImageView picture1 = (ImageView) findViewById(R.id.result_pic_1);
TextView textView2 = (TextView) findViewById(R.id.result_text_2);
ImageView picture2 = (ImageView) findViewById(R.id.result_pic_2);
TextView textView3 = (TextView) findViewById(R.id.result_text_3);
ImageView picture3 = (ImageView) findViewById(R.id.result_pic_3);



Button send = (Button)findViewById(R.id.send);
textView1.setText("try_1:hhh");
textView2.setText("try2:我买了一个iphonexxx");
textView3.setText("try3:ttt");
Intent intent = this.getIntent();
byte buffer[] = intent.getByteArrayExtra("image");
bm = BitmapFactory.decodeByteArray(buffer,0,buffer.length); // 解码文件
Log.w("TAG", "size: " + bm.getByteCount() + " width: " + bm.getWidth() + " heigth:" + bm.getHeight()); // 输出图像数据

picture1.setImageBitmap(bm);
picture2.setImageBitmap(bm);
picture3.setImageBitmap(bm);

send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SendPicture().execute(bm);
}
});
//Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
//picture1.setImageBitmap(bitmap);
}
class SendPicture extends AsyncTask<Bitmap,Void,Bitmap>{
private static final String SOCKET_IP = "10.30.73.82";
private static final int SOCKET_PORT = 8000;
private Bitmap bmp;
private DataOutputStream dataOutputStream = null;
private ByteArrayOutputStream out = null;
private DataInputStream dataInputStream = null;
private ByteArrayInputStream in = null;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {
Bitmap bitmap = bitmaps[0];
try{
Socket socket = new Socket(SOCKET_IP,SOCKET_PORT);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
byte[] sendBytes = out.toByteArray();
int size = sendBytes.length;
dataOutputStream.writeInt(size);
dataOutputStream.flush();
dataOutputStream.write(sendBytes);
dataOutputStream.flush();
//dataOutputStream.close();
dataInputStream = new DataInputStream(socket.getInputStream());
try {
size = dataInputStream.readInt();
}catch (IOException e){
e.printStackTrace();
}
byte[] receiveBytes = new byte[size];
int hasReceived = 0;
while (hasReceived <size){
hasReceived+=dataInputStream.read(receiveBytes,hasReceived,size-hasReceived);
}
bmp = BitmapFactory.decodeByteArray(receiveBytes, 0,receiveBytes.length);



}catch (Exception e){
e.printStackTrace();
}

return bmp;
}

@Override
protected void onPostExecute(Bitmap bmp) {
textView1.setText("try_1:hhh");

}
}
}


做了个app,要从服务器接收一个图片然后显示。
现在我在onPostExecute中调用picture1.setImageBitmap(bmp)
显示找不到picture1这个变量,为什么啊,更新UI再onPostExecute里面没错吧?!
而且如果在里面调用textView1.setText("try_1:hhh");也是显示没有找到变量

总之,问题就是在onPostExecute里无法更新UI,找不到之前定义的变量?
...全文
268 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡牧 2019-03-23
  • 打赏
  • 举报
回复
变量作用域都没搞懂的可以写出这个代码还是比较6的。
jzp12 2019-03-23
  • 打赏
  • 举报
回复
先搞清楚什么是变量作用域
雕·不懒惰 2019-03-22
  • 打赏
  • 举报
回复
+1
控件基本上都是定义成全局变量的吧
luan1010 2019-03-22
  • 打赏
  • 举报
回复
你的控件都是onCreate()中的局部变量,怎么可能找的到

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧