[Solved] Problem with IntentReceiver

Put your problem here if it does not fit any of the other categories.

[Solved] Problem with IntentReceiver

Postby sasuke » Tue Feb 19, 2008 10:14 am

Hello all,

I'm trying to make a game that communicate via GTalkService.
For example: player 1 moved up, player 1 send msg to player 2 'player 1 moved UP'. Then player 2 received the msg via IntentReceiver, read it, and redraw the player 1 position (in player 2 screen).

The foreground activity is the game which is interacting with user, and the receiver works in background.

Image

When i received an intent, i got an error like this :
"[font=Courier New]Unable to instantiate receiver dj.android.tes.myCanvas$msgReceiver:java.lang.InstantiationException : dj.android.tes.myCanvas$msgReceiver[/font]"

What's the problem?

Here's my code:

main.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package dj.android.tes;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class main extends Activity {
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     public void onCreate(Bundle icicle) {
  10.         super.onCreate(icicle);
  11.         setContentView(new myCanvas(this));
  12.     }
  13. }
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


myCanvas.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package dj.android.tes;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.IntentReceiver;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.view.View;
  10.  
  11.  
  12. public class myCanvas extends View {
  13.         private int y = 10;
  14.         private Context c;
  15.         myCanvas(Context ctx){
  16.                 super(ctx);
  17.                 c=ctx;
  18.         }
  19.  
  20.         @Override
  21.         public void draw(Canvas canvas) {
  22.         Paint paint = new Paint();
  23.         canvas.drawColor(Color.WHITE);
  24.         paint.setColor(Color.BLUE);
  25.         paint.setStrokeCap(Paint.Cap.SQUARE);
  26.         paint.setStrokeWidth(10);
  27.         canvas.drawRect(200, y, 220, y+20, paint);
  28.         }
  29.        
  30.         public class msgReceiver extends IntentReceiver {
  31.             static final String ACTION = "myIntent.intent.action.MY_CUSTOM_INTENT";
  32.             public void onReceiveIntent(Context context, Intent intent) {
  33.                 if (intent.getAction().equals(ACTION)) {
  34.                         //y++
  35.                         //invalidate
  36.                 }
  37.             }
  38.         }
  39.        
  40. }
  41.  
  42.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.         package="dj.android.tes">
  4.         <application android:icon="@drawable/icon">
  5.                 <activity android:label="@string/app_name"
  6.                         android:name="main">
  7.                         <intent-filter>
  8.                                 <action android:name="android.intent.action.MAIN" />
  9.                                 <category
  10.                                         android:name="android.intent.category.LAUNCHER" />
  11.                         </intent-filter>
  12.                 </activity>
  13.                 <receiver android:name="myCanvas$msgReceiver">
  14.                         <intent-filter>
  15.                                 <action
  16.                                         android:name="myIntent.intent.action.MY_CUSTOM_INTENT" />
  17.                         </intent-filter>
  18.                 </receiver>
  19.         </application>
  20. </manifest>
  21.  
  22.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Thanks,
sasuke
Last edited by sasuke on Sun Feb 24, 2008 6:40 am, edited 1 time in total.
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

Postby AndroidBorkCreature » Tue Feb 19, 2008 1:41 pm

In
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver android:name="myCanvas$msgReceiver">
  2.  
  3.                <intent-filter>
  4.  
  5.                     <action
  6.  
  7.                         android:name="myIntent.intent.action.MY_CUSTOM_INTENT" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


you did not specify a receiver class. You must put:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver class=" myCanvasmsgReceiver">
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


instead of

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver android:name="myCanvas$msgReceiver">
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


All the best!
Paul
AndroidBorkCreature
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 05, 2008 1:56 pm

Postby sasuke » Tue Feb 19, 2008 2:05 pm

Hi paul,

The class= parameter throughout the AndroidManifest.xml file has been renamed, replace all instances of class= with android:name=.


In M5, the class replaced by android:name... you can check it in Android SDK M5 Release - API Changes Overview... :wink: :wink:

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Tue Feb 19, 2008 5:32 pm

Hello sasuke,

perhaps the problem is, that you defined as a inner class. If I remember right: to instantiate a inner class you need an existing object of the outer class.
Can you receive the Intent when you put it to Top-level (own file) :?: The rest of the code would have to be adapted later on.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Wed Feb 20, 2008 1:22 am

Hello plusminus,

Of course it'll work if i defined the IntentReceiver in it's own file. But the problem is, when the onReceiveIntent get called, how do i redraw the canvas? (see the img from my 1st post here). e.g Oh.. i got msg player 1 moved up, so i should redraw the position of player 1 on my canvas.. ?

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Wed Feb 20, 2008 10:44 am

Hello sasuke,

you only need a reference to that canvas. Then you could use Getters & Setters.

Since Version m5 the SDK provides the following function for android.view.View:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         boolean         post(Runnable action)
  2.         Causes the Runnable to be added to the message queue.
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


So you can even post a Runnable to execute the changes.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby sasuke » Wed Feb 20, 2008 11:33 am

Thank you plusminus, i'll try it.....
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby sasuke » Wed Feb 20, 2008 1:57 pm

Hello plusminus,

I saw your PizzaTimer, and i thought i could use the method there (haven't use the view.post yet..)
I'm trying to trigger the draw method from the onClick, when i trace the code, method setY and getY from myView get called, but the view won't redraw...

main
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class main extends Activity {
  3.  
  4.         private myView mv;
  5.  
  6.     /** Called when the activity is first created. */
  7.  
  8.     @Override
  9.  
  10.     public void onCreate(Bundle icicle) {
  11.  
  12.         super.onCreate(icicle);
  13.  
  14.         LinearLayout lin = new LinearLayout(this);
  15.  
  16.         lin.setOrientation(LinearLayout.VERTICAL);
  17.  
  18.         Button but = new Button(this);        
  19.  
  20.         but.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  21.  
  22.         but.setText("update view");
  23.  
  24.         but.setOnClickListener(listener);
  25.  
  26.        
  27.  
  28.         mv = new myView(this);
  29.  
  30.         mv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
  31.  
  32.         lin.addView(but);
  33.  
  34.         lin.addView(mv);
  35.  
  36.         setContentView(lin);
  37.  
  38.     }
  39.  
  40.    
  41.  
  42.     Handler myHandler = new Handler(){
  43.  
  44.         @Override
  45.  
  46.         public void handleMessage(Message msg) {
  47.  
  48.                 mv.setY(mv.getY()+100);
  49.  
  50.                 mv.invalidate();
  51.  
  52.                 super.handleMessage(msg);      
  53.  
  54.         }
  55.  
  56.     };
  57.  
  58.  
  59.  
  60.     OnClickListener listener = new OnClickListener() {
  61.  
  62.         public void onClick(View v) {
  63.  
  64.                 Message m = new Message();
  65.  
  66.                 myHandler.sendMessage(m);
  67.  
  68.         }
  69.  
  70.     };
  71.  
  72. }
  73.  
  74.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


myView
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class myView extends View{
  3.  
  4.         private int y = 10;
  5.  
  6.         private Context c;
  7.  
  8.         myView(Context context)
  9.  
  10.         {
  11.  
  12.                 super(context);
  13.  
  14.                 c = context;
  15.  
  16.         }
  17.  
  18.        
  19.  
  20.         @Override
  21.  
  22.         public void draw(Canvas canvas) {
  23.  
  24.         Paint paint = new Paint();
  25.  
  26.         canvas.drawColor(Color.WHITE);
  27.  
  28.         paint.setColor(Color.BLUE);
  29.  
  30.         paint.setStrokeCap(Paint.Cap.SQUARE);
  31.  
  32.         paint.setStrokeWidth(10);
  33.  
  34.         canvas.drawRect(200, y, 120, y+20, paint);
  35.  
  36.         }
  37.  
  38.        
  39.  
  40.         public void setY(int yval) {y = yval;}
  41.  
  42.         public int getY() {return y;}
  43.  
  44. }
  45.  
  46.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby sasuke » Wed Feb 20, 2008 2:18 pm

ew... this one didn't work either... :cry: :cry:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     private Handler mHandler = new Handler();
  3.  
  4.    
  5.  
  6.     private Runnable mUpdateTimeTask = new Runnable() {
  7.  
  8.            public void run() {
  9.  
  10.                    mv.setY(mv.getY()+100);
  11.  
  12.                    mv.invalidate();
  13.  
  14.            }
  15.  
  16.         };
  17.  
  18.  
  19.  
  20.     OnClickListener listener = new OnClickListener() {
  21.  
  22.         public void onClick(View v) {
  23.  
  24.             mHandler.removeCallbacks(mUpdateTimeTask);
  25.  
  26.             mHandler.post(mUpdateTimeTask);
  27.  
  28.         }
  29.  
  30.     };
  31.  
  32.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Wed Feb 20, 2008 7:01 pm

Hello sasuke,

please attach your full project, I'll have a look.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Thu Feb 21, 2008 12:41 am

Thank you plusminus

Regards,
sasuke
Attachments
handler.rar
(67.95 KiB) Downloaded 111 times
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Thu Feb 21, 2008 1:13 am

tomorrow, its just after 1[sup]am[/sup] now ;)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Thu Feb 21, 2008 1:27 am

ok. that's fine..
it's 7.26 AM in here... :lol:
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Thu Feb 21, 2008 12:37 pm

Hello sasuke,

:lol: its onDraw() not draw() :oops:

The following code works fine. (Btw, I would not name my Activity "main". I renamed it.)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="dj.android.handler">
  6.  
  7.     <application android:icon="@drawable/icon">
  8.  
  9.         <activity android:label="@string/app_name" android:name="MainApp">
  10.  
  11.             <intent-filter>
  12.  
  13.                 <action android:name="android.intent.action.MAIN" />
  14.  
  15.                 <category android:name="android.intent.category.LAUNCHER" />
  16.  
  17.             </intent-filter>
  18.  
  19.         </activity>
  20.  
  21.     </application>
  22.  
  23. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package dj.android.handler;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.View.OnClickListener;
  12.  
  13. import android.widget.Button;
  14.  
  15. import android.widget.LinearLayout;
  16.  
  17.  
  18.  
  19. public class MainApp extends Activity {
  20.  
  21.         protected myView mv = null;
  22.  
  23.        
  24.  
  25.     /** Called when the activity is first created. */
  26.  
  27.     @Override
  28.  
  29.     public void onCreate(Bundle icicle) {
  30.  
  31.         super.onCreate(icicle);
  32.  
  33.         LinearLayout lin = new LinearLayout(this);
  34.  
  35.         lin.setOrientation(LinearLayout.VERTICAL);
  36.  
  37.         Button but = new Button(this);        
  38.  
  39.         but.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  40.  
  41.         but.setText("update view");
  42.  
  43.        
  44.  
  45.         mv = new myView(this);
  46.  
  47.         mv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
  48.  
  49.         lin.addView(but);
  50.  
  51.         lin.addView(mv);
  52.  
  53.         setContentView(lin);
  54.  
  55.        
  56.  
  57.         but.setOnClickListener(new OnClickListener() {
  58.  
  59.                 public void onClick(View v) {
  60.  
  61.                 MainApp.this.mv.post(MainApp.this.mv.mUpdateTimeTask);
  62.  
  63.                 }
  64.  
  65.         });
  66.  
  67.     }
  68.  
  69. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package dj.android.handler;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.graphics.Canvas;
  8.  
  9. import android.graphics.Color;
  10.  
  11. import android.graphics.Paint;
  12.  
  13. import android.view.View;
  14.  
  15.  
  16.  
  17. public class myView extends View {
  18.  
  19.  
  20.  
  21.         private int y = 10;
  22.  
  23.         private Context c;
  24.  
  25.        
  26.  
  27.         public Runnable mUpdateTimeTask = new Runnable() {
  28.  
  29.                 public void run() {
  30.  
  31.                         myView.this.y += 100;
  32.  
  33.                         myView.this.invalidate();
  34.  
  35.                 }
  36.  
  37.         };
  38.  
  39.  
  40.  
  41.         myView(Context context) {
  42.  
  43.                 super(context);
  44.  
  45.                 c = context;
  46.  
  47.         }
  48.  
  49.  
  50.  
  51.         @Override
  52.  
  53.         public void onDraw(Canvas canvas) {
  54.  
  55.                 Paint paint = new Paint();
  56.  
  57.                 canvas.drawColor(Color.WHITE);
  58.  
  59.                 paint.setColor(Color.BLUE);
  60.  
  61.                 paint.setStrokeCap(Paint.Cap.SQUARE);
  62.  
  63.                 paint.setStrokeWidth(10);
  64.  
  65.                 canvas.drawRect(200, y, 120, y + 20, paint);
  66.  
  67.                 c.showAlert("redrawn", 0, "y:" + y, "ok", true);
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         public void setY(int yval) {
  74.  
  75.                 y = yval;
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.         public int getY() {
  82.  
  83.                 return y;
  84.  
  85.         }
  86.  
  87. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Thu Feb 21, 2008 3:03 pm

omg.... :oops: :oops:
so silly..... how could i do that... :lol: :lol:

what's the difference : mainApp.this.mv.post and mv.post (without mainApp.this) ? optimization?

Regards,
sasuke

nb: so, that's the picture of our great admin? 8)
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests