如何在主布局中添加由另一个线程控制的 imageView?
我正在为 Android 制作一个应用程序。我通过单个主线程按下按钮就达到了我的目标! (在 ImageView 中显示保存在 SD 上的图像)但我需要使用线程来完成此操作以节省一些时间,因为我将创建其他线程。
当我在新线程上执行此操作时,会出现一个警告,告诉我:
“只有创建视图层次结构的原始线程才能触及它的 意见。”
并且图像未打开。
这是这段代码:
public class intsocketclient extends Activity implements OnClickListener{
public ImageView imagen;
private Button connectPhones;
private Handler conectarhandler = null;
private Runnable conectarunner = null;
public boolean condicion = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagen = (ImageView) findViewById(R.id.imagen);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(this);
conectarhandler = new Handler();
conectarunner = new Runnable() {
public void run() {
conectayenvia();
conectarhandler.post(this);
}
};
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub.
if(v==connectPhones) {
new Thread (conectarunner).start();
}
}
public void conectayenvia () {
if (condicion){
condicion = false;
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");
imagen.setImageBitmap(bMap);
}
}
}
但我真的需要它是这样的。
是否可以采用主布局(原始的“main.xml”)和某种“添加”它是另一个主文件(一个仅包含 imageView 的“threadmain.xml”),而且还具有按第一个原始“main.xml”布局的按钮和其他类型的功能??????
I'm making an app for Android. I have reached my goal with the single main thread pushing a button!!! (show an image saved on the SD in an ImageView) But I need to do it with threading to save some time and because I will make other threads.
The problem when I do this on a new thread a warning appears that tells me:
"Only the original thread that created a view hierachy can touch its
views."
And the Image is not opened.
Here is this code:
public class intsocketclient extends Activity implements OnClickListener{
public ImageView imagen;
private Button connectPhones;
private Handler conectarhandler = null;
private Runnable conectarunner = null;
public boolean condicion = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagen = (ImageView) findViewById(R.id.imagen);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(this);
conectarhandler = new Handler();
conectarunner = new Runnable() {
public void run() {
conectayenvia();
conectarhandler.post(this);
}
};
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub.
if(v==connectPhones) {
new Thread (conectarunner).start();
}
}
public void conectayenvia () {
if (condicion){
condicion = false;
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");
imagen.setImageBitmap(bMap);
}
}
}
But I really need it to be this way.
Is it possible to take the main layout (the original "main.xml") and some kind of "add" over it another main file (a "threadmain.xml" which only contains the imageView)but also with the capability of pushing buttons and other kind of things of the first original "main.xml" layout????????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用AsyncTask,因为它会自动处理线程,AsyncTask的preExecute和postExecute方法在UI线程上运行。
在 onClick 中调用 asyncTask。
use AsyncTask, as it will handle threads automatically, the preExecute and postExecute methods of AsyncTask run on UI thread.
call the asyncTask in onClick.
您的问题是您尝试不在 UI 线程上运行 UI 方法。解决方案是调用 runOnUiThread 来自另一个线程。
Your problem is that you're trying to run UI methods not on the UI thread. The solution is to call runOnUiThread from the other thread.