Calling view class

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Calling view class

Postby ansh.chauhan » Tue May 27, 2008 12:28 pm

HEy buddy wats up....

i have made an activity Name Infotainment
and i hav created it object likt this

Infotainment info;

in this class i have created on more class SampleView which extends View

and i called this class by using

setContentView(new SampleView(this));

its work fine.....

in Sample View class i hav created key pressed event ....on keyDown()

In that event i call new view class mainmenu();

by using.....

info.setContentView(new MainMenu(info));

where in MainMenu i hav create a constructor lik this

MainMenu(Context context) {
super(context);
Log.v("Rahul", "Rahul");
// TODO Auto-generated constructor stub

}


but its giving null pointer exception......can u help me plzzzzzz about this.....
when i check logcat is showing "Rahul" but null pointer exception also
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Top

Postby Jesmiatka » Tue May 27, 2008 2:13 pm

Hi,

I don't think that's the position what throws the null-pointer exception..
You might want to check you stack-trace of the null-pointer to see what line and class it is thrown in.

Greetz,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby ansh.chauhan » Wed May 28, 2008 6:08 am

I dont kno where i wil check stack but in log cat it showing lik this


DEBUG/dalvikvm(692): Exception Ljava/lang/NullPointerException; from ViewRoot.java:964 not caught locally
DEBUG/dalvikvm(692): Exception Ljava/lang/NullPointerException; from ZygoteInit.java:1553 not caught locally
DEBUG/AndroidRuntime(692): Shutting down VM
WARN/dalvikvm(692): threadid=3: thread exiting with uncaught exception (group=0x4000fdf8)
ERROR/AndroidRuntime(692): Uncaught handler: thread Main exiting due to uncaught exception
ERROR/AndroidRuntime(692): java.lang.NullPointerException
ERROR/AndroidRuntime(692): at info.ui.InfotainmentActivity$SampleView.onKeyDown(InfotainmentActivity.java:92)
ERROR/AndroidRuntime(692): at android.view.KeyEvent.dispatch(KeyEvent.java:493)
ERROR/AndroidRuntime(692): at android.view.View.dispatchKeyEvent(View.java:2689)
ERROR/AndroidRuntime(692): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:530)
ERROR/AndroidRuntime(692): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:530)
ERROR/AndroidRuntime(692): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:530)
ERROR/AndroidRuntime(692): at android.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1189)
ERROR/AndroidRuntime(692): at android.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:871)
ERROR/AndroidRuntime(692): at android.app.Activity.dispatchKeyEvent(Activity.java:1503)
ERROR/AndroidRuntime(692): at android.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1164)
ERROR/AndroidRuntime(692): at android.view.ViewRoot.deliverKeyEvent(ViewRoot.java:926)
ERROR/AndroidRuntime(692): at android.view.ViewRoot.handleMessage(ViewRoot.java:595)
ERROR/AndroidRuntime(692): at android.os.Handler.dispatchMessage(Handler.java:80)
ERROR/AndroidRuntime(692): at android.os.Looper.loop(Looper.java:91)
ERROR/AndroidRuntime(692): at android.app.ActivityThread.main(ActivityThread.java:3052)
ERROR/AndroidRuntime(692): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(692): at java.lang.reflect.Method.invoke(Method.java:356)
ERROR/AndroidRuntime(692): at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1547)
ERROR/AndroidRuntime(692): at android.os.ZygoteInit.main(ZygoteInit.java:1445)
ERROR/AndroidRuntime(692): at android.dalvik.NativeStart.main(Native Method)
INFO/Process(508): Sending signal. PID: 692 SIG: 3
INFO/dalvikvm(692): threadid=7: reacting to signal 3
INFO/dalvikvm(692): DALVIK THREADS:
INFO/dalvikvm(692): "Main" prio=5 tid=3 NATIVE
INFO/dalvikvm(692): | group="main" sCount=1 dsCount=0 s=0 obj=0x40010450
INFO/dalvikvm(692): | sysTid=496 nice=0 sched=0/0 handle=-1091285764
INFO/dalvikvm(692): at android.os.BinderProxy.transact(Native Method)
INFO/dalvikvm(692): at android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:1441)
INFO/dalvikvm(692): at android.os.RuntimeInit.crash(RuntimeInit.java:250)
INFO/dalvikvm(692): at android.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:56)
INFO/dalvikvm(692): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:853)
INFO/dalvikvm(692): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:850)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): at android.dalvik.NativeStart.main(Native Method)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): "Binder Thread #2" prio=5 tid=13 NATIVE
INFO/dalvikvm(692): | group="main" sCount=1 dsCount=0 s=0 obj=0x400a94a8
INFO/dalvikvm(692): | sysTid=705 nice=-15 sched=0/0 handle=1316592
INFO/dalvikvm(692): at android.dalvik.NativeStart.run(Native Method)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): "Binder Thread #1" prio=5 tid=11 NATIVE
INFO/dalvikvm(692): | group="main" sCount=1 dsCount=0 s=0 obj=0x40076920
INFO/dalvikvm(692): | sysTid=704 nice=-15 sched=0/0 handle=1313288
INFO/dalvikvm(692): at android.dalvik.NativeStart.run(Native Method)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): "JDWP" daemon prio=5 tid=9 VMWAIT
INFO/dalvikvm(692): | group="system" sCount=1 dsCount=0 s=0 obj=0x4001fe68
INFO/dalvikvm(692): | sysTid=695 nice=0 sched=0/0 handle=1309968
INFO/dalvikvm(692): at android.dalvik.NativeStart.run(Native Method)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): "Signal Catcher" daemon prio=5 tid=7 RUNNABLE
INFO/dalvikvm(692): | group="system" sCount=0 dsCount=0 s=0 obj=0x4001d700
INFO/dalvikvm(692): | sysTid=694 nice=0 sched=0/0 handle=1298856
INFO/dalvikvm(692): at android.dalvik.NativeStart.run(Native Method)
INFO/dalvikvm(692): (break frame)
INFO/dalvikvm(692): "HeapWorker" daemon prio=5 tid=5 VMWAIT
INFO/dalvikvm(692): | group="system" sCount=1 dsCount=0 s=0 obj=0x4001bc80
INFO/dalvikvm(692): | sysTid=693 nice=0 sched=0/0 handle=1296256
INFO/dalvikvm(692): at android.dalvik.NativeStart.run(Native Method)
INFO/dalvikvm(692): (break frame)
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby ansh.chauhan » Wed May 28, 2008 6:09 am

where my code is lik this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package info.ui;
  3.  
  4.  
  5.  
  6. import java.io.ByteArrayInputStream;
  7.  
  8. import java.io.IOException;
  9.  
  10. import java.io.InputStreamReader;
  11.  
  12.  
  13.  
  14. import parser.ParseEvent;
  15.  
  16. import parser.Xml;
  17.  
  18. import parser.XmlParser;
  19.  
  20. import android.app.Activity;
  21.  
  22. import android.content.Context;
  23.  
  24. import android.graphics.Bitmap;
  25.  
  26. import android.graphics.BitmapFactory;
  27.  
  28. import android.graphics.Canvas;
  29.  
  30. import android.graphics.Color;
  31.  
  32. import android.graphics.Paint;
  33.  
  34. import android.os.Bundle;
  35.  
  36. import android.util.Log;
  37.  
  38. import android.view.KeyEvent;
  39.  
  40. import android.view.View;
  41.  
  42.  
  43.  
  44. public class InfotainmentActivity extends Activity {
  45.  
  46.         /** Called when the activity is first created. */
  47.  
  48.         Color color;
  49.  
  50.         String myString = "Test: ";
  51.  
  52.         public static Bitmap bm;
  53.  
  54.          public static InfotainmentActivity activity;
  55.  
  56.        
  57.  
  58.  
  59.  
  60.         @Override
  61.  
  62.         public void onCreate(Bundle icicle) {
  63.  
  64.                 super.onCreate(icicle);
  65.  
  66.  
  67.  
  68.                 sendRecieveData();
  69.  
  70.  
  71.  
  72.                 setContentView(new SampleView(this));
  73.  
  74.  
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         public void sendRecieveData() {
  81.  
  82.  
  83.  
  84.                 Connection connection = new Connection(
  85.  
  86.                                 "http://www.migital.com/AdsCompaign/GetSplash.aspx?Uid=6&AppId=1&PId=1",
  87.  
  88.                                 1);
  89.  
  90.                 processDataList(connection.getData());
  91.  
  92.  
  93.  
  94.         }
  95.  
  96.  
  97.  
  98.         public static class SampleView extends View implements Runnable {
  99.  
  100.  
  101.  
  102.                 public Paint mPaint = new Paint();
  103.  
  104.  
  105.  
  106.                 SampleView(Context context) {
  107.  
  108.  
  109.  
  110.                         super(context);
  111.  
  112.                         this.setFocusable(true);
  113.  
  114.                         // TODO Auto-generated constructor stub
  115.  
  116.                         Connection connection = new Connection(AppConstant.splashUrl, 2);
  117.  
  118.                         ByteArrayInputStream mainImageinputStream = new ByteArrayInputStream(
  119.  
  120.                                         connection.getData());
  121.  
  122.                         bm = BitmapFactory.decodeStream(mainImageinputStream);
  123.  
  124.                         Thread t = new Thread(this);
  125.  
  126.                         t.start();
  127.  
  128.                 }
  129.  
  130.  
  131.  
  132.                 @Override
  133.  
  134.                 protected void onDraw(Canvas canvas) {
  135.  
  136.                         Log.v("Anshu", "5");
  137.  
  138.                         canvas.drawColor(Color.WHITE);
  139.  
  140.  
  141.  
  142.                         if (bm != null)
  143.  
  144.                                 canvas.drawBitmap(bm, 30, 50, mPaint);
  145.  
  146.                         mPaint.setColor(Color.BLUE);
  147.  
  148.                         canvas.drawText("Press Any Key", 40, 110, mPaint);
  149.  
  150.                         Log.v("Anshu", "7");
  151.  
  152.                 }
  153.  
  154.  
  155.  
  156.                 @Override
  157.  
  158.                 public void run() {
  159.  
  160.                         // TODO Auto-generated method stub
  161.  
  162.                         Log.v("Anshu", "123");
  163.  
  164.                         //      postInvalidate();
  165.  
  166.  
  167.  
  168.                 }
  169.  
  170.                 public boolean onKeyDown(int keyCode, KeyEvent event) {
  171.  
  172.                         //              keyCode=event.getKeyCode();
  173.  
  174. //                      if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){
  175.  
  176.                                
  177.  
  178.                                 Log.v("12345", "12345");
  179.  
  180. //                              MainMenu mainMenu=new MainMenu(activity);
  181.  
  182.                                
  183.  
  184.                                         activity.setContentView(new MainMenu(activity));
  185.  
  186.                                        
  187.  
  188.                        
  189.  
  190. //                      }
  191.  
  192.                         return super.onKeyDown(keyCode, event);
  193.  
  194.                 }
  195.  
  196.                 }
  197.  
  198.  
  199.  
  200.         public void processDataList(byte[] xmlData) {
  201.  
  202.  
  203.  
  204.                 ByteArrayInputStream bis = new ByteArrayInputStream(xmlData);
  205.  
  206.  
  207.  
  208.                 InputStreamReader isr = new InputStreamReader(bis);
  209.  
  210.  
  211.  
  212.                 XmlParser parser = null;
  213.  
  214.                 try {
  215.  
  216.  
  217.  
  218.                         parser = new XmlParser(isr);
  219.  
  220.  
  221.  
  222.                         ParseEvent event = null;
  223.  
  224.                         while ((event = parser.read()).getType() != Xml.END_DOCUMENT) {
  225.  
  226.  
  227.  
  228.                                 if (event.getType() == Xml.START_TAG
  229.  
  230.                                                 && event.getName().equals("Data")) {
  231.  
  232.  
  233.  
  234.                                         ParseEvent nextevent = null;
  235.  
  236.                                         while ((nextevent = parser.peek()).getType() != Xml.END_DOCUMENT) {
  237.  
  238.  
  239.  
  240.                                                 String name = nextevent.getName();
  241.  
  242.  
  243.  
  244.                                                 int type = nextevent.getType();
  245.  
  246.                                                 if (type == Xml.END_TAG && name.equals("Data")) {
  247.  
  248.  
  249.  
  250.                                                         nextevent = null;
  251.  
  252.                                                         name = null;
  253.  
  254.                                                         break;
  255.  
  256.                                                 }
  257.  
  258.                                                 nextevent = parser.read();
  259.  
  260.                                                 if (type != Xml.START_TAG) {
  261.  
  262.  
  263.  
  264.                                                         nextevent = null;
  265.  
  266.                                                         continue;
  267.  
  268.                                                 }
  269.  
  270.                                                 ParseEvent next = parser.read();
  271.  
  272.                                                 if (next.getType() != Xml.TEXT) {
  273.  
  274.  
  275.  
  276.                                                         nextevent = null;
  277.  
  278.                                                         next = null;
  279.  
  280.                                                         continue;
  281.  
  282.                                                 }
  283.  
  284.                                                 if (name.equals("logo"))
  285.  
  286.                                                         AppConstant.logUrl = "http://www.migital.com/AdsCompaign/"
  287.  
  288.                                                                         + next.getText();
  289.  
  290.                                                 if (name.equals("splash"))
  291.  
  292.                                                         AppConstant.splashUrl = "http://www.migital.com/AdsCompaign/"
  293.  
  294.                                                                         + next.getText();
  295.  
  296.  
  297.  
  298.                                                 nextevent = null;
  299.  
  300.                                                 next = null;
  301.  
  302.                                         }
  303.  
  304.  
  305.  
  306.                                 }
  307.  
  308.                         }
  309.  
  310.                 } catch (IOException ex) {
  311.  
  312.                         ex.printStackTrace();
  313.  
  314.                 }
  315.  
  316.  
  317.  
  318.         }
  319.  
  320. }
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby ansh.chauhan » Wed May 28, 2008 6:10 am

this is the class which i m calling

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package info.ui;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.graphics.Canvas;
  9.  
  10. import android.util.Log;
  11.  
  12. import android.view.View;
  13.  
  14.  
  15.  
  16. public class MainMenu extends View{
  17.  
  18.  
  19.  
  20.         MainMenu(Context context) {
  21.  
  22.                 super(context);
  23.  
  24.                 Log.v("Rahuul", "Rahuul");
  25.  
  26.                 // TODO Auto-generated constructor stub
  27.  
  28.                
  29.  
  30.         }
  31.  
  32.         @Override
  33.  
  34.         protected void onDraw(Canvas canvas) {
  35.  
  36.                 Log.v("qqqqq", "qqqqqq");
  37.  
  38.         }
  39.  
  40.  
  41.  
  42. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby Jesmiatka » Wed May 28, 2008 8:06 am

Hi,

you've never initialized the activity field, it is still null. Hence you get the null-pointer.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //This is null
  2.  
  3. public static InfotainmentActivity activity;
  4.  
  5.  
  6.  
  7. //And then you do this
  8.  
  9. activity.setContentView(new MainMenu(activity));
  10.  
  11.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Greetz,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Top

Postby ansh.chauhan » Wed May 28, 2008 8:27 am

then how can i use my activity class
how can i call view class
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby Jesmiatka » Wed May 28, 2008 8:32 am

Hi,

use this as your onCreate of InfotainmentActivity.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.      @Override
  3.  
  4.      public void onCreate(Bundle icicle) {
  5.  
  6.           super.onCreate(icicle);
  7.  
  8.  
  9.  
  10.           sendRecieveData();
  11.  
  12.  
  13.  
  14.           activity = this;
  15.  
  16.  
  17.  
  18.           setContentView(new SampleView(this));
  19.  
  20.  
  21.  
  22.      }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby ansh.chauhan » Wed May 28, 2008 8:34 am

but wat is activity here......
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby ansh.chauhan » Wed May 28, 2008 8:38 am

hey buddy u hav msn or yahoo or gtalk id so we can talk dere..becoz i m too much new in android...and i didn understand nything plzz help me
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby Jesmiatka » Wed May 28, 2008 8:42 am

Hi,

I'm using your own code and you do not know what it is :P
I reckon that code works now, does it?

Anyway you actually should use the context that you pass to the SampleView. You save the context passed to that class in a var (F/E: mContext) and set it on the constructor. This context makes it possible to execute things on the screen.
This will change your code to:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public static class SampleView extends View implements Runnable {
  2.  
  3.  
  4.  
  5.           public Paint mPaint = new Paint();
  6.  
  7.           public Context mContext;
  8.  
  9.  
  10.  
  11.           SampleView(Context context) {
  12.  
  13.                super(context);
  14.  
  15.                this.setFocusable(true);
  16.  
  17.                mContext = context;
  18.  
  19.                Connection connection = new Connection(AppConstant.splashUrl, 2);
  20.  
  21.                ByteArrayInputStream mainImageinputStream = new ByteArrayInputStream(
  22.  
  23.                          connection.getData());
  24.  
  25.                bm = BitmapFactory.decodeStream(mainImageinputStream);
  26.  
  27.                Thread t = new Thread(this);
  28.  
  29.                t.start();
  30.  
  31.           }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


So the other activity's context will be mContext in that SampleView.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           public boolean onKeyDown(int keyCode, KeyEvent event) {
  2.  
  3.  
  4.  
  5.                          mContext.setContentView(new MainMenu(mContext));
  6.  
  7.  
  8.  
  9.                return super.onKeyDown(keyCode, event);
  10.  
  11.           }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


You use the context of the activity that interacts with the screen for executing other things on the screen.
You might want to learn more about Android by going to the http://code.google.com/Android/

Greetz,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby ansh.chauhan » Wed May 28, 2008 10:51 am

hey thanx buddy now its working......

but i m in deep trouble plzzzz help....i m new in Android and my company gave me ultimatum to complete the project in 5 days otherwise they wil fired me so plzzzzzzzzz help me tell me from where i wil study android.the link u hva given to me is not working
and i hav checked all the tutorial they r very confusing...i m not able to understand...
thats y i was asking ur chating id....plzzzzzzz help me buddy....
or if dere ny job releted with android plz tell me

thanx..
Anshul
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Postby Jesmiatka » Wed May 28, 2008 10:55 am

Hi,

this is the link, had a wrong case in it:
http://code.google.com/android/

Greetz,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby ansh.chauhan » Wed May 28, 2008 11:02 am

heyi hav check this.....but its very hard to undestand everything dere....

code is too much confusing...
hey tell me one more thing......

i hva images and i want to make list of those image how i can do is dere ny list class
and how we can handle keycode in that.....

and is android support Runnable method of multi threading
ansh.chauhan
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Tue May 27, 2008 7:04 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests