Android:无法显示多张图片

标签 android uiimage image-manipulation

我有以下主类和线程-TCP 客户端。客户端循环运行,接收消息并将其传递给主类。在主类中,我解析消息并尝试根据收到的消息的名称和值显示不同的图像。 例如:shiftDirection1 名称:shiftDirection & 值:1

但是我只能显示第一条收到的消息对应的图片,其余收到的消息对应的图片无法显示。

请仔细阅读下面的代码,并提出错误/问题和替代方法。

感谢您的时间和努力。

马杜

主类:

public class TCPListen extends Activity implements TCPListener {
    private TextView mTitle;
    public String recData[] = new String[2];
    String PresentGear = "0";   

    /** Called when the activity is first created. */
     @Override  
         public void onCreate(Bundle savedInstanceState) {  
             super.onCreate(savedInstanceState);                 

             TcpServiceHandler handler = new TcpServiceHandler(this,this);  
             Thread th = new Thread(handler);  
             th.start();      
         }          

         public String[] callCompleted(String source){ 
                //Log.d("TCP", "Std parser " + source);
                //mTitle.setText(source);
                //String data[] = new String[2]; 

                //if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>"))  {           
                    Document doc = null;  
                    try{
                       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
                       DocumentBuilder db = dbf.newDocumentBuilder();  
                       doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));  
                       NodeList n = doc.getElementsByTagName("N");  
                       Node nd = n.item(0);  
                       String msgName = nd.getFirstChild().getNodeValue();  
                       NodeList n1 = doc.getElementsByTagName("V");  
                       Node nd1 = n1.item(0);  
                       String tmpVal = nd1.getFirstChild().getNodeValue();  
                       recData[0] = msgName;  
                       recData[1] = tmpVal;
                       if (recData[0].equals("currGear"))   PresentGear = recData[1];
                       Log.d("TCP", "Inside Std parser " + recData[0] + " " + recData[1]);
                       actionOnData(recData[0], recData[1]);
                      }  
                    catch(Exception e){  
                    e.printStackTrace();  
                }                   
                Log.d("TCP", "Just outside Std parser " + recData[0] + " " + recData[1]);
                return recData;
                //} else Log.d("TCP", "Message in wrong format " + source);
                //mTitle.setText("Message in wrong format " + source);
                //return data;
            }


       //Function to display driver messages/images based on individual messages
         public void actionOnData(String name, String value) {
            String tempName = name;
            String tempVal = value; 
            setContentView(R.layout.image);
            ImageView showImage = (ImageView) findViewById(R.id.imageView1);
            //Log.d("TCP", "------------>" + tempName + "  " + tempVal);            

            if (tempName.equals("shiftDirection") && tempVal.equals("1")) {
                //setContentView(R.layout.image);
                //TextView text_top = (TextView) findViewById(R.id.textView1);
                //showImage = (ImageView) findViewById(R.id.imageView1);            
                //text_bottom.setText(Info[1]);
                showImage.setImageResource(R.drawable.shift_up);
                Log.d("TCP", "1------------>" + showImage);
             } else if (tempName.equals("shiftDirection") && tempVal.equals("-1")) {                 
                //setContentView(R.layout.image);
                //TextView text_bottom = (TextView) findViewById(R.id.textView2);
                //Resources res = getResources();
                //Drawable drawable = res.getDrawable(R.drawable.shift_down);

                //showImage = (ImageView) findViewById(R.id.imageView1);            
                //text_bottom.setText(Info[1]);
                showImage.setImageResource(R.drawable.shift_down);              
             } else if (tempName.equals("recomGear") && tempVal != null) {
                 Log.d("TCP", "3------------>" + tempName + "  " + tempVal);
                Integer msgValue = Integer.parseInt(recData[1]);
                //Integer CurrentGear = (msgValue) - 1;
                //Log.d("TCP","in DA Images. Current gear: " + CurrentGear);
                //String Gear = Integer.toString(CurrentGear);
                setContentView(R.layout.image);
                TextView text_top = (TextView) findViewById(R.id.textView1);
                TextView text_bottom = (TextView) findViewById(R.id.textView2);
                showImage = (ImageView) findViewById(R.id.imageView1);          
                showImage.setImageResource(R.drawable.shift_up);
                text_bottom.setText(PresentGear);
                text_top.setText(tempVal);
             } else if (tempName.equals("currGear") && tempVal != null) {
                 Log.d("TCP", "4------------>" + tempName + "  " + tempVal);
                PresentGear = tempVal;          
                //Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
                setContentView(R.layout.image);
                TextView text_bottom = (TextView) findViewById(R.id.textView2);
                text_bottom.setText(PresentGear);                       
             } else if (tempName.equals("shiftDirection") && tempVal.equals("0")) {
                 Log.d("TCP", "5------------>" + tempName + "  " + tempVal);
                Log.d("TCP","in DA Images. Present gear: " + PresentGear);
                setContentView(R.layout.image);
                TextView text_bottom = (TextView) findViewById(R.id.textView2);
                //TextView text_top = (TextView) findViewById(R.id.textView1);
                //text_top.setText("Go on");  
                text_bottom.setText(PresentGear);           
             }          
          }
}

只显示第一个if case对应的图片。程序控制进入第二个 if 循环但不显示图像。

接口(interface):

public interface TCPListener {
    public String[] callCompleted(String msg);
}

线程(TCP 客户端):

public class TcpServiceHandler implements Runnable {  
     TCPListener _listener;            
     private Activity _act; 
     public BufferedReader in;
     public TcpServiceHandler(TCPListener listener, Activity act){    
         _listener = listener;  
         _act = act;  
     }        

     public synchronized void run() {  
         // TODO Auto-generated method stub           
         //if(socket==null){    
             try {  
                 //InetAddress serverAddr = InetAddress.getByName("192.168.178.25");  
                 Socket socket = new Socket("192.168.62.23", 1200, true);  
         //  
                 //while(true){  
                  try {                           
                         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
                         final int delay = 100;
                         final Timer _timer = new Timer();
                         _timer.scheduleAtFixedRate(new TimerTask() {                       
                            public void run(){
                                String str;
                                try {
                                    str = in.readLine();
                                    _listener.callCompleted(str);
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                            }
                         }, 0, delay); 
                         //final String str = in.readLine();  
                         //this._act.runOnUiThread(new Runnable(){  

                         //public void run() {  
                        //   _listener.callCompleted(str);  
                           //  }                                
                         //});                                                  
                  }  
                  catch(Exception e){  
                      e.printStackTrace();  
                  }  
                 //}  
             } catch (UnknownHostException e) {  
                 // TODO Auto-generated catch block  
                 e.printStackTrace();  
             } catch (IOException e) {  
                 // TODO Auto-generated catch block  
                 e.printStackTrace();  
             }  
     }    
 }

最佳答案

您可能会检查两件事:

  1. 您的TcpServiceHandler 是线程的Runnablerun() 中没有循环。您在此方法中定义的 _timer 可能在它完成工作之前就已经死了。

  2. 您是从后台线程更改 UI 吗?这通常不是一个好主意。

  3. 检查 AsyncTask,这是一个在后台运行操作的便捷工具。

关于Android:无法显示多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4865617/

相关文章:

Android 蓝牙打印在 4.1 上停止工作

android - 以编程方式将我的设备或应用程序的时间与服务器同步?

image-processing - 如何在SO上生成用户ID图像?

Android 将多个位图图 block 流式传输到单个 jpeg 文件

安卓 : How to get SIM ID of both SIM in Dual SIM mobile

javascript - 出现键盘时如何在Android中处理网站

iphone - 从 CIImage 获取 UIImage 无法正常工作

ios - 以编程方式创建更高分辨率的 PNG 图像

cocoa-touch - 反转具有透明背景的图像的颜色

iphone - iOS:将 6x6 400x300px 图像添加到一张合成图像