Problem on upgrading the code to suit the latest sdk

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Problem on upgrading the code to suit the latest sdk

Postby totw » Fri Apr 10, 2009 1:08 pm

I was previously using sdk m3-rc20a. I thought it would take a lot of time, but surprisingly, there are only a few coding errors as follows :

1)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void handleMessage(android.os.Message msg) {
  2.  
  3.                 HashMap<String, String> hm = (HashMap<String, String>) msg.getData();
  4.  
  5.  
  6.  
  7.                 String msgText = hm.get("message");
  8.  
  9. .
  10.  
  11. .
  12.  
  13. .
  14.  
  15. .
  16.  
  17. .
  18.  
  19.  
  20.  
  21. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


the error is, cannot cast from bundle to HashMap<String,String>

2)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Message StringToMessage(String s) {
  2.  
  3.                 Message msg = new Message();
  4.  
  5.                 msg.what = 0;
  6.  
  7.                 HashMap<String, String> hm = new HashMap<String, String>();
  8.  
  9.                 hm.put("message", s);
  10.  
  11.                 msg.setData(hm);
  12.  
  13.                 return msg;
  14.  
  15.         }
  16.  
  17.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


the error is, The method setData(Bundle) in the type Message is not applicable for the arguments (HashMap<String,String>)
I guess, these errors are related to the HashMap hm..

3)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TerminalView extends SurfaceView {
  2.  
  3. .
  4.  
  5. .
  6.  
  7. .
  8.  
  9. .
  10.  
  11. .
  12.  
  13. .
  14.  
  15.         public TerminalView(Context context) {
  16.  
  17.                 super(context);
  18.  
  19.                 // TODO Auto-generated constructor stub
  20.  
  21.         }
  22.  
  23.  
  24.  
  25.         public TerminalView(Context context, AttributeSet attrs, Map inflateParams) {
  26.  
  27.                 super(context, attrs, inflateParams);
  28.  
  29.                 // TODO Auto-generated constructor stub
  30.  
  31.         }
  32.  
  33.  
  34.  
  35.         public TerminalView(Context context, AttributeSet attrs, Map inflateParams,
  36.  
  37.                         int defStyle) {
  38.  
  39.                 super(context, attrs, inflateParams, defStyle);
  40.  
  41.                 // TODO Auto-generated constructor stub
  42.  
  43.         }
  44.  
  45.         .
  46.  
  47. .
  48.  
  49. .
  50.  
  51. .
  52.  
  53. .
  54.  
  55. .
  56.  
  57. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The following lines in the code above are showing errors :

super(context, attrs, inflateParams)

is showing

The constructor SurfaceView(Context, AttributeSet, Map) is undefined

and

super(context, attrs, inflateParams, defStyle);

is showing

The constructor SurfaceView(Context, AttributeSet, Map, int) is undefined


please suggest suitable modifications to make the code suitable to the latest sdk...thks.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Top

Postby padde » Fri Apr 10, 2009 1:52 pm

This should fix the first problem....
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void handleMessage(Message msg) {
  3.  
  4.     Serializable serialized = msg.getData().getSerializable("hashmap");
  5.  
  6.     HashMap<String, String> hm = (HashMap<String, String>) serialized;
  7.  
  8.     String msgText = hm.get("message");
  9.  
  10.     .
  11.  
  12.     .
  13.  
  14.     .
  15.  
  16. }
  17.  
  18.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


this the second one.....
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private Message StringToMessage(String s) {
  3.  
  4.     Message msg = new Message();
  5.  
  6.     msg.what = 0;
  7.  
  8.     HashMap<String, String> hm = new HashMap<String, String>();
  9.  
  10.     hm.put("message", s);
  11.  
  12.     Bundle bundle = new Bundle();
  13.  
  14.     bundle.putSerializable("hashmap", hm);
  15.  
  16.     msg.setData(bundle);
  17.  
  18.     return msg;
  19.  
  20. }
  21.  
  22.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and this for the last one
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. TerminalView(Context context) {
  3.  
  4.     super(context);
  5.  
  6. }
  7.  
  8. TerminalView(Context context, AttributeSet attrs) {
  9.  
  10.     super(context, attrs);
  11.  
  12. }
  13.  
  14. TerminalView(Context context, AttributeSet attrs, int defStyle) {
  15.  
  16.     super(context, attrs, defStyle);
  17.  
  18. }
  19.  
  20.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby totw » Fri Apr 10, 2009 3:23 pm

Thank you for your reply.
I did whatever you suggested and now all the errors are gone.

However the application is not working as intended.

I use a button to trigger this class which is an activity.
some of the relevant code of that class is as below...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. .
  3.  
  4. .
  5.  
  6. .
  7.  
  8. .
  9.  
  10. class MyHandler extends Handler {
  11.  
  12.         TerminalView tv;
  13.  
  14.         TextView txt;
  15.  
  16.  
  17.  
  18.         MyHandler(TerminalView termView, TextView txt) {
  19.  
  20.                 super();
  21.  
  22.                 tv = termView;
  23.  
  24.                 this.txt = txt;
  25.  
  26.         }
  27.  
  28.  
  29.  
  30.        
  31.  
  32.         public void handleMessage(android.os.Message msg) {
  33.  
  34.                 Serializable serialized = msg.getData().getSerializable("hashmap");
  35.  
  36.                 HashMap<String, String> hm = (HashMap<String, String>) serialized;
  37.  
  38.  
  39.  
  40.                 String msgText = hm.get("message");
  41.  
  42.                 if(msg.what == 0)
  43.  
  44.                 {
  45.  
  46.                         tv.cp.AddBuffer(msgText.toCharArray());
  47.  
  48.                         tv.invalidate(0,0,Integer.MAX_VALUE,Integer.MAX_VALUE);
  49.  
  50.                         txt.setVisibility(View.INVISIBLE);
  51.  
  52.                 }
  53.  
  54.                 else if(msg.what == 1)
  55.  
  56.                 {
  57.  
  58.                         txt.setText(msgText);
  59.  
  60.                         txt.setTextColor(0xFFFF0000);
  61.  
  62.                         txt.setVisibility(View.VISIBLE);                       
  63.  
  64.                 }
  65.  
  66.                 else if(msg.what == 2)
  67.  
  68.                 {
  69.  
  70.                         txt.setText(msgText);
  71.  
  72.                         txt.setTextColor(0xFF00FF00);
  73.  
  74.                         txt.setVisibility(View.VISIBLE);                       
  75.  
  76.                 }
  77.  
  78.         }
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. class TerminalSocket implements Runnable {
  89.  
  90.         public Thread tr;
  91.  
  92.         private MyHandler hnd;
  93.  
  94.         protected Socket sk = null;
  95.  
  96.         private InputStream iStream = null;
  97.  
  98.         public OutputStream oStream = null;
  99.  
  100.         public byte X = 1;
  101.  
  102.         public byte Y = 1;
  103.  
  104.         protected String Server = "";
  105.  
  106.         protected int Port = 0;
  107.  
  108.        
  109.  
  110.        
  111.  
  112.        
  113.  
  114.         public TerminalSocket(MyHandler h) {
  115.  
  116.                 hnd = h;
  117.  
  118.                 tr = new Thread(this);
  119.  
  120.         }
  121.  
  122.        
  123.  
  124.        
  125.  
  126.  
  127.  
  128.         public void SocketStart() {
  129.  
  130.                 tr.start();
  131.  
  132.         }
  133.  
  134.  
  135.  
  136.         private Message StringToMessage(String s) {
  137.  
  138.                 Message msg = new Message();
  139.  
  140.                 msg.what = 0;
  141.  
  142.                 HashMap<String, String> hm = new HashMap<String, String>();
  143.  
  144.                 hm.put("message", s);
  145.  
  146.                 Bundle bundle = new Bundle();
  147.  
  148.             bundle.putSerializable("hashmap", hm);
  149.  
  150.                 msg.setData(bundle);
  151.  
  152.                 return msg;
  153.  
  154.         }
  155.  
  156.        
  157.  
  158.         private Message ErrorToMessage(String s) {
  159.  
  160.                 Message msg = new Message();
  161.  
  162.                 msg.what = 1;
  163.  
  164.                 HashMap<String, String> hm = new HashMap<String, String>();
  165.  
  166.                 hm.put("message", s);
  167.  
  168.                 Bundle bundle = new Bundle();
  169.  
  170.             bundle.putSerializable("hashmap", hm);
  171.  
  172.                 msg.setData(bundle);
  173.  
  174.                
  175.  
  176.                 return msg;
  177.  
  178.         }
  179.  
  180.        
  181.  
  182.         private Message InfoToMessage(String s) {
  183.  
  184.                 Message msg = new Message();
  185.  
  186.                 msg.what = 2;
  187.  
  188.                 HashMap<String, String> hm = new HashMap<String, String>();
  189.  
  190.                 hm.put("message", s);
  191.  
  192.                 Bundle bundle = new Bundle();
  193.  
  194.             bundle.putSerializable("hashmap", hm);
  195.  
  196.                 msg.setData(bundle);
  197.  
  198.                 return msg;
  199.  
  200.         }
  201.  
  202.  
  203.  
  204.         public void run() {
  205.  
  206.                
  207.  
  208.                 InetAddress ia = null;
  209.  
  210.                 hnd.sendMessage(InfoToMessage("Resolve server name " + Server));
  211.  
  212.                 try {
  213.  
  214.                         ia = InetAddress.getByName(Server);
  215.  
  216.                 } catch (UnknownHostException ex) {
  217.  
  218.                         hnd.sendMessage(ErrorToMessage("Unknown host " + Server));
  219.  
  220.                         return;
  221.  
  222.                 }
  223.  
  224.                 hnd.sendMessage(InfoToMessage("Connecting to "  + Server + ":" + Port));
  225.  
  226.                 try {
  227.  
  228.                         sk = new java.net.Socket(ia, Port);
  229.  
  230.                 } catch (IOException ex) {
  231.  
  232.                         hnd.sendMessage(ErrorToMessage("Socket time out for " + Server + ":" + Port));
  233.  
  234.                         return;
  235.  
  236.                 }
  237.  
  238.  
  239.  
  240.                 try {
  241.  
  242.                         iStream = sk.getInputStream();
  243.  
  244.                         oStream = sk.getOutputStream();
  245.  
  246.                 } catch (IOException ex) {
  247.  
  248.                         hnd.sendMessage(ErrorToMessage("Protocol error for " + Server + ":" + Port));
  249.  
  250.                         return;
  251.  
  252.                 }
  253.  
  254. .
  255.  
  256. .
  257.  
  258. .
  259.  
  260. .
  261.  
  262.  
Parsed in 0.050 seconds, using GeSHi 1.0.8.4



when i was using the older sdk, after triggering the class
i was getting the msg : "connecting to <servername and port>"
which would later connect to a server.

but now after changing the code
I directly get the message caused by IOException : "Socket timeout for <servername>"
and does not proceed at all.

I'm sure this is happening because of the modifications made to the code. But I'm unable to find a relevant substitute for functions corresponding to the newer sdk in order to exactly match what i had done in the previous sdk.

any suggestions on this would be appreciated. thanx a lot.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby padde » Fri Apr 10, 2009 4:25 pm

i would suggest to try a different port and/or check if the InetAddress in "ia" is reachable.


This works fine for me (SDK 1.1 r1)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int Port = 80;
  3.  
  4. String Server = "java.sun.com";
  5.  
  6.  
  7.  
  8. InetAddress ia = null;
  9.  
  10. try {
  11.  
  12.     ia = InetAddress.getByName(Server);
  13.  
  14.     Socket sk = new java.net.Socket(ia, Port);
  15.  
  16. } catch (UnknownHostException e) {
  17.  
  18.     e.printStackTrace();
  19.  
  20. } catch (IOException e) {
  21.  
  22.     e.printStackTrace();
  23.  
  24. }
  25.  
  26.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby totw » Fri Apr 10, 2009 7:12 pm

thanx...

I tried that but didn't solve my problem. I debugged the code and found that after the execution of line :

sk = new java.net.Socket(ia, Port);

it gives the error :

com.sun.jdi.InvocationException occurred invoking method.

I checked all other variables etc. everything else is ok (i mean variables contain the desired values...)
i think its failing to instantiate the socket.

any suggestions??
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby padde » Sat Apr 11, 2009 1:26 am

I googled a bit .. but found nothing that would help.
I am quite sure that it isnt android related at all.

Here is the description of that error.. but its pretty meaningless
http://www.lumentier.com/java/ext/jdk-7-bld1/com.sun.jdi/InvocationException/class-javadoc.lmtr

Most of the google findings referring to a "toString()" method in conjunction with that error.
Hopefully i could point you in the right direction.. if not i think it requires more code to really help.


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Postby totw » Sat Apr 11, 2009 5:11 am

Thank you for your help. I really appreciate it.

I think the problem is solved. I just added a permission in the manifest and the error is gone.

<uses-permission android:name = "android.permission.INTERNET"/>

I haven't tested the full functioning of the application yet but with the error gone atleast I can go a step further.

if problem persists, i will post more code.


Thanx a lot.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby totw » Sat Apr 11, 2009 10:57 am

hi.

I would like you to take a look at the very first problem I had posted in this topic and the solutions you suggested.
The first two problems are totally gone, thanks to you. But there is something wrong about the solution of the third one.

Ofcourse all the syntactical errors are gone but its not functioning as it supposed to.

I mean after execution of that code it should display command prompt of the server prompting for username and password; but all i get is a blank screen.

The TerminalView class which extends SurfaceView the second and the third constructor or the TerminalView class call the super() method which is the constructor for the SuraceView class. I think that the surfaceview class in the newer sdk has different arguments for its constructor. So the map variable that I have taken fails to match the arguments of this constructor. I am unable to figure out what to do, but i think the map variable is essential.

The code is large in size so I'm not posting it here. Instead I have attached the files to this post. I would really appreciate if you took a look at them and tell me whats wrong.
Thank you.
Last edited by totw on Sun Apr 26, 2009 8:38 am, edited 1 time in total.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby padde » Sat Apr 11, 2009 12:13 pm

Hmm "CharPanel" and "TerminalConstants" are missing.. without them nothing works
and replacing them with stubs could take forever :)


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby totw » Sat Apr 11, 2009 12:25 pm

This is not the entire code.

The project consists of 10 classes. I have given you these two classes from the entire project because the error is from these two classes for sure.

The CharPanel and TerminalConstants classes are there in my project, I just didn't post them here because i'm sure that there is no error in the rest of the project.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby padde » Sat Apr 11, 2009 1:11 pm

I thought as much.. so i can just look instead of compile and try.
Based on your error description and the provided code it might have
something to do about the surfaceview itself. The surfaceview canvas
isnt instant ready.. it could take some ms until its ready to draw.
In THIS thread
i have some code that take care of the same issue. Look at the MjpegView and especially
the surface.callback methods and the thread start timing.
My guess is that the content you estimate is there but at that time the surfaceview isnt ready
and stays blank.

Greets
Padde

--- Edit ----
Okay.. another.. maybe stupid idea came up to my mind.
Check if the color you set before the drawText statement isnt black.
Maybe you just writing black on black :D
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby totw » Sat Apr 11, 2009 1:35 pm

The same thing that u guessed is taking place.. when the application connects to the destination machine, the message "connecing to .." disappears and instead of showing the destination machine prompt, there is a black screen.. so maybe that part of the view is not getting displayed but it is there..

I'll check out the code u have posted..

thks a lot :)
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby totw » Sat Apr 11, 2009 6:47 pm

Just to be sure that the error lies in displaying the contents and not anything else, I tested my application ...

I set up a telnet server running Fedora (Fedora shows a popup window when anyone connects to it through say telnet).

I ran my application on windows .. After I entered the IP address, the same thing happened.. the screen was black again..

Assuming that my application was functioning properly (except for the corresponding display) I typed in the username on my emulator (TELNET requires the server to have a username and password)..

After pressing enter, a popup appeared on my server confirming my doubt that everything else was working fine except the display..

I also did a blindfold entering of some commands (like creating a directory etc ).. these commands were executed successfully too..

So, now I have to figure out the problem in the display... I'm posting the xml file corresponding to the TerminalActivity class :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:layout_width="fill_parent"
  6.  
  7.         android:layout_height="fill_parent">
  8.  
  9.        
  10.  
  11.         <tel.net.client.TerminalView
  12.  
  13.                 android:id="@+id/terminalview"
  14.  
  15.                 android:layout_width="fill_parent"
  16.  
  17.        android:layout_height="fill_parent"
  18.  
  19.        android:visibility="invisible"
  20.  
  21.    />
  22.  
  23.      
  24.  
  25.         <RelativeLayout
  26.  
  27.                 android:layout_width="fill_parent"
  28.  
  29.                 android:layout_height="fill_parent" >
  30.  
  31.                
  32.  
  33.                 <TextView
  34.  
  35.                         android:id="@+id/txt1"
  36.  
  37.                         android:visibility="visible"
  38.  
  39.                         android:layout_width="wrap_content"
  40.  
  41.                         android:layout_height="wrap_content"
  42.  
  43.                         android:layout_centerInParent="true"
  44.  
  45.                        
  46.  
  47.                         android:textColor="#FF00FF00"
  48.  
  49.                         android:textStyle="bold"
  50.  
  51.                         android:textSize="16sp"/>
  52.  
  53.         </RelativeLayout>
  54.  
  55.  
  56.  
  57. </FrameLayout>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4





..thks for all your help, padde :)
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Postby padde » Sun Apr 12, 2009 1:08 pm

Well.. the layout file looks fine to me. I still think its a timing problem.
To test this implement the callbacks like i did in my mjpeg code.
Some debug outputs in "surfaceCreated" method and in your method where
you draw your stuff should show if there is a timing problem. If "surfaceCreated"
is called AFTER your draw method than the surface wasnt ready and therefore you
cant see anything.
I am not quite sure if those visibility changes also could be a problem. Maybe the surface
needs to be created anytime you set it back to be visible.

I will try to reduce the code you provided to a running minimum so i can play with it .. an
hopefully fix it :)

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby padde » Sun Apr 12, 2009 4:52 pm

Okay.. i think i got it. I dont know exactly why but the draw and onDraw method in your TerminalView never gets invoked.. even after an invalidate or postInvalidate. But the fix for this is simple.
When you want to update your TerminalView just add "tv.onDraw(tv.getHolder().lockCanvas());" right before
"tv.invalidate();" .. if i properly understand your code this happens just on one location in your "MyHandler" message handler when "message.what" is 0.
In your TerminalView change "public void draw(Canvas canvas)" to "protected void onDraw(Canvas canvas)"
and add "getHolder().unlockCanvasAndPost(canvas);" as last line in this method.. just before the method returns
(yes after those for loops).

What we have done now is to call onDraw man-made and take care of the state of the corresponding canvas of
the SurfaceHolder.
I really want to know why this isnt called from android itself.. but for now this will work just fine.

Ps. You should fly over your thread code.. and stop it at the right time.


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top
Next

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 7 guests