Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Bob Kerns » Tue, 20 Apr 2010 17:18:58 GMT


Sponsored Links
 I do this, without any issues at all.

How are you getting your Application instance from your activities?
not getApplicationContext() I hope...

Have you examined the state from the debugger? There may be something
you're missing about the state of things.



> thread



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by patbenatar » Tue, 20 Apr 2010 18:04:18 GMT


 Bob-

Nope, I'm using getApplication()

For instance [inside an Activity]:


MyApplication application;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    application = (MyApplication) this.getApplication();
}


The error is most easily re-produceable when I am in Activity B, after
A started it for result, and then click a button in B to launch Maps
with an Intent to search for an address, then from Maps I hit Home
then my launcher icon on my Home Screen to go back into my app--BAM!
Activity B fails onResume with a NullPointerException coming from a
getter method on my MyApplication class, when I know for sure Activity
A properly set the data in MyApplication..

So strange...

Thanks,
Nick






> > thread


Sponsored Links


Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by westmeadboy » Tue, 20 Apr 2010 19:05:50 GMT


 Not sure if this helps, but use
 http://developer.android.com/reference/java/lang/System.html #identityHashCode(java.lang.Object)
to check whether the Application object is indeed the same in both
cases (or whether they are in different DVMs)





> > > thread



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Mark Murphy » Tue, 20 Apr 2010 19:47:52 GMT


 


A database, flat file, or other persistent store is the proper place for
it, perhaps cached via a custom Application class or static data members.

> 



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by String » Tue, 20 Apr 2010 20:59:03 GMT


 



Sure, that's the normal app lifecycle. But if this is the case, why
isn't the Activity's onCreate being called? To me, that's the
confusing part of this.

String

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Mark Murphy » Tue, 20 Apr 2010 21:04:00 GMT


 




Whoops. I missed that part. If that's happening, that is very strange.
The Application object should be the second-to-last thing to go -- if
activities are in RAM, so should the Application, AFAIK.

That being said, the *last* thing to go is static data members, since
they'll only get cleaned up when the whole Dalvik VM is reset or the
process is fully terminated. It may be worth a test to see if a static
data member survives all of this.

Something definitely is not adding up.

My apologies for missing that part of the original post and not
addressing it in my earlier reply.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 2.0 Available!

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Christopher » Wed, 21 Apr 2010 00:48:53 GMT


 Just out of curiosity, is your Activity making use of onNewIntent()?
I was curious because I recently worked on an Activity that did, and
found that many variables in the Activity were set to null after this
method completed.  It's almost as if onNewIntent does a quick reset of
the instance, which given the nature of onNewIntent could make sense.
I might not be 100% accurate in terms of what's going on, but thought
perhaps the information could help.


(Note: I think I originally replied only to the author, so I attempted
to reconstruct the post as best I could.  I apologize if suddenly
there are two almost identical posts from me that show up).


Christopher

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by patbenatar » Wed, 21 Apr 2010 09:36:41 GMT


 Thanks for all the input guys--

Mark, it is indeed very strange that my Activity's onCreate is not
being called here. And the reason I have chosen not to use a database
or flat file is because the lifetime of this data is a maximum of 20
minutes before my app rules it outdated and fetches new data from the
server. Didn't really see the point in writing data with such a short
shelf life to a more permanent data store. If my app is destroyed and
relaunched within this 20 minute period, I have no problem with doing
a fresh API fetch (why not? the more frequently [while remaining un-
invasive] I can update the data, the better)... I will throw

Christopher, nope not overring onNewIntent--sounds like some weird
behavior you've run into there...

Is it possible that a memory leak could possibly be the cause of this
behavior? I had some leaks previously but went through all my code a
couple weeks ago and believe I have removed them all but I haven't
completely confirmed this [although my app hasn't hit any OutOfMemory
errors since then]. I don't see why a memory leak would be removing
data though, normally they causes data to pile up...

I have found one issue in my code that may resolve this.. I
implemented a potential fix but have not had a chance to test on my
device yet. Will post back. In the meanwhile, anyone else with
thoughts on this matter, please share :)

Nick





>



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Dianne Hackborn » Wed, 21 Apr 2010 11:23:50 GMT


 Fwiw, there is definitely no clearing of instance variables when
onNewIntent() is called.  I can't imagine how the framework would even clear
instance variables, except for using reflection to find them and poke them,
which we definitely don't do.

For these situations it may be useful to look at the event log (adb logcat
-b events) to see what the system is doing, and also possibly the activity
manager state (adb shell dumpsys activity) to see the state the system has
your activities in.




>



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Bob Kerns » Thu, 22 Apr 2010 03:59:39 GMT


 Or use the debugger, and set breakpoints in each method on the
affected class, and step through until you see where the field is
being cleared.




> >



Other Threads

1. Bluetooth file transfer to Laptop is failing.

Bluetooth file transfer to mobile (any set) is successful. But if I
tried to transfer to Laptop it is failing. Below is the log for
successful and unsuccessful case

For unsuccessful case, it is unable to get channel number. Whether i
have to do settings for laptop case?

Phone to Mobile(Motorola) transfer success:
BluetoothShare.DIRECTION_OUTBOUND
V/BtOppTransfer( 1405): startConnectSession IN
V/BluetoothOppPreference( 1405): getChannel 00:17:E2:8D:76:C4_1105
V/BluetoothOppPreference( 1405): getChannel for 00:17:E2:8D:76:C4_1105
as null
V/BtOppTransfer( 1405): doOpushSdp IN
I/AppsUtility( 1236): [apps_filldata]raw data=609
I/AppsUtility( 1236): [apps_update_zero_point][apps_update_zero_point]:
[609,483,608]
I/AppsUtility( 1236): [apps_check_TDS]raw data: x=609, y=483, z=608;
zero point: x=608, y=482, z=607; delta data: dx=0, dy=0, dz=0, state=0
I/AppsUtility( 1236): [ELAN_HandleEvent]dx=0, dy=0, dz=0, state=0
I/AppsUtility( 1236): [apps_filldata]raw data=483
I/AppsUtility( 1236): [apps_filldata]raw data=608
I/AppsUtility( 1236): [apps_filldata]raw data=515
I/AppsUtility( 1236): [apps_filldata]raw data=12
I/AppsUtility( 1236): [apps_filldata]raw data=32
V/BtOppTransfer( 1405): Do Opush SDP request for address 00:17:E2:8D:
76:C4
D/BluetoothDevice( 1405): getServiceChannel IN
D/BtOppTransfer( 1405): Get OPUSH channel 8 from SDP for 00:17:E2:8D:
76:C4
V/BtOppTransfer( 1405): startConnectSession OUT
V/BtOppTransfer( 1405): SDP request returned 8 (2 ms)


Phone to Laptop: Unsuccessful case.
 V/BtOppTransfer( 1423): BluetoothShare.DIRECTION_OUTBOUND
V/BtOppTransfer( 1423): startConnectSession IN
V/BluetoothOppPreference( 1423): getChannel 00:25:56:CD:5D:13_1105
V/BluetoothOppPreference( 1423): getChannel for 00:25:56:CD:5D:13_1105
as null
V/BtOppTransfer( 1423): doOpushSdp IN
V/BtOppTransfer( 1423): Do Opush SDP request for address 00:25:56:CD:
5D:13
D/BluetoothDevice( 1423): getServiceChannel IN
V/BtOppTransfer( 1423): Remote Service channel not in cache
D/BluetoothDevice( 1423): fetchUuidsWithSdp IN
V/BtOppTransfer( 1423): Start new SDP, wait for result
V/BtOppTransfer( 1423): doOpushSdp OUT-1
V/BtOppTransfer( 1423): startConnectSession OUT

..
..
..

V/BtOppTransfer( 1423): onReceive IN
V/BtOppTransfer( 1423): ACTION_UUID for device 00:25:56:CD:5D:13
V/BtOppTransfer( 1423): device.equals(mBatch.mDestination)
V/BtOppTransfer( 1423): uuid != null
V/BtOppTransfer( 1423): onReceive OUT
V/BtOppTransfer( 1423): SDP request returned -1 (4268 ms)

Thanks
Guru

-- 

2. ScrollView is not calling onDraw()

Hello,

I hope this question belongs in the Android Developers and not Android
Beginners group but I have a question about ScrollViews. I am
currently working with a customized GraphView class and am trying to
put it into a ScrollView but it doesn't seem to work. Whenever I run
the code, it comes up with a blank screen. I've written the code in
XML and also tried to create the ScrollView encapsulating the
GraphView programmatically but whenever the code runs, it comes up
with just a blank screen and GraphView's onDraw() is never called.

Here's the code (with XML file):

GraphView.java:
public class GraphView extends View {

     private Paint paint;
     private float[] values;
     private String[] horlabels;
     private String[] verlabels;
     private String title;

     public GraphView(Context context, AttributeSet attributeSet) {
          super(context, attributeSet);

          title = "Graph";
          float[] values = new float[] { 2.0f, 1.5f, 2.5f, 1.0f,
3.0f };
          String[] verlabels = new String[] { "great", "ok", "bad" };
          String[] horlabels = new String[] { "today", "tomorrow",
"next week", "next month" };

          if (values == null)
               values = new float[0];
          else
               this.values = values;

          if (horlabels == null)
               this.horlabels = new String[0];
          else
               this.horlabels = horlabels;
          if (verlabels == null)
               this.verlabels = new String[0];
          else
               this.verlabels = verlabels;
          paint = new Paint();
     }

     @Override
     protected void onDraw(Canvas canvas) {
          float border = 20;
          float horstart = border * 2;
          float height = getHeight();
          float width = getWidth() - 1;
          float max = getMax();
          float min = getMin();
          float diff = max - min;
          float graphheight = height - (2 * border);
          float graphwidth = width - (2 * border);

          paint.setTextAlign(Align.LEFT);
          int vers = verlabels.length - 1;
          for (int i = 0; i < verlabels.length; i++) {
               paint.setColor(Color.DKGRAY);
               float y = ((graphheight / vers) * i) + border;
               canvas.drawLine(horstart, y, width, y, paint);
               paint.setColor(Color.WHITE);
               canvas.drawText(verlabels[i], 0, y, paint);
          }
          int hors = horlabels.length - 1;
          for (int i = 0; i < horlabels.length; i++) {
               paint.setColor(Color.DKGRAY);
               float x = ((graphwidth / hors) * i) + horstart;
               canvas.drawLine(x, height - border, x, border, paint);
               paint.setTextAlign(Align.CENTER);
               if (i==horlabels.length-1)
                    paint.setTextAlign(Align.RIGHT);
               if (i==0)
                    paint.setTextAlign(Align.LEFT);
               paint.setColor(Color.WHITE);
               canvas.drawText(horlabels[i], x, height - 4, paint);
          }

          paint.setTextAlign(Align.CENTER);
          canvas.drawText(title, (graphwidth / 2) + horstart, border -
4, paint);

          float colwidth = (width - (horstart)) / (horlabels.length -
1);
          float lasth = 0;
          for (int i = 0; i < values.length; i++) {
               float val = values[i] - min;
               float rat = val / diff;
               float h = graphheight * rat;
               if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + horstart,
(border - lasth) + graphheight, (i * colwidth) + horstart, (border -
h) + graphheight, paint);
               lasth = h;
          }
     }

     private float getMax() {
          float largest = Integer.MIN_VALUE;
          for (int i = 0; i < values.length; i++)
               if (values[i] > largest)
                    largest = values[i];
          return largest;
     }

     private float getMin() {
          float smallest = Integer.MAX_VALUE;
          for (int i = 0; i < values.length; i++)
               if (values[i] < smallest)
                    smallest = values[i];
          return smallest;
     }
}


Graph.java:
public class Graph extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <com.examples.graph.GraphView android:layout_width="fill_parent"
          android:layout_height="fill_parent"
     />
</ScrollView>



Any help would be greatly appreciated! Thanks!

-- 

3. How to make my app start up itself at the specified time just like the alarm clock.

4. How to add a View in Gallery?

5. Hotspot in ImageView

6. grow or shrink view

7. Access a Service across multiple Applications