I am getting NullPointer exception for my app when switching from potrait mode to landscape mode and vice versa

by manoj » Wed, 11 Feb 2009 13:02:22 GMT


Sponsored Links
 Hi friends,

I have written a small example which displays the photos from the
server.

Its working fine when the device is in one of the mode (landscape of
potrait ).

But if I switch to landscape to potrait or potrait  to landscape while
downloading the photos from the server,

I am getting NullPointerException, showing with
androidRunTimeException.

Can any one please help to solve this problem?

I am using LinearLayout -> ImageView

I am using threads to download the images.

Thanks,
Manoj.
--~--~---------~--~----~------------~-------~--~----~



I am getting NullPointer exception for my app when switching from potrait mode to landscape mode and vice versa

by clark » Wed, 11 Feb 2009 13:15:16 GMT


 You may want to try adding android:configChanges="orientation" to
AndroidManifest.xml and overriding onConfigurationChanged().  If a
configuration change is not specified in android:configChanges then
Android will take care of it for you, which means restarting your
app.  Without seeing code it is hard to say exactly what you should do
to handle this, but the information I listed should get you started.
I'd do a search regarding onConfigurationChanged() in the SDK
documentation.

I hope that gets you going in the right direction.


~clark



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


I am getting NullPointer exception for my app when switching from potrait mode to landscape mode and vice versa

by Mark Murphy » Wed, 11 Feb 2009 21:59:38 GMT


 


Without the source code for the area where the exception is occurring,
and knowing which line is the one raising the exception, it is rather
difficult to give you a precise solution.

Bear in mind that, on a rotation like this, by default your existing
activity is destroyed and a new one created.

Here is a series of blog posts on handling rotation events that may help
you understand what is going on:

 http://androidguys.com/?s=rotational +forces

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. How can LinearLayout know child's focusable?

Hi. Expert!

I have a LinearLayout that have 4 buttons children. so I like to know
whether my button is focusable or not when I press a arrow key. and I
like to know whether my button is clicked or not when I click a child
button.  LinearLayout.onFocusChanged function is not called when child
has focus.
So I tried to find it.  below code is my difficult and dirty solution.
in below code, I should call button.setOnFocusChangeListener and
button.setOnClickListener on every child button. If I have a hundred
button, I should call button.setOnFocusChangeListener. and I don't
need to know what button is exactly focuschanged. I just need to know
whether my child buttons is focusChanged or not. and

what is a clear and simple solution?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.windriver.Test.MyLayout
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true">
    </com.windriver.Test.MyLayout>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:focusable="true" />
</LinearLayout>

public class MyLayout extends LinearLayout implements
View.OnFocusChangeListener, View.OnClickListener {
    static final String TAG = "MyLayout";

    public MyLayout (Context context) {
        this (context, null);
    }

    public MyLayout (Context context, AttributeSet attrs) {
        super (context, attrs);

        LayoutParams lp = new LayoutParams (LayoutParams.WRAP_CONTENT,
 
LayoutParams.WRAP_CONTENT);

        Button button;
        for (int i = 0; i < 4; i++) {
            button = new Button (context);
            button.setText (String.format ("Button %d", i));
            button.setOnFocusChangeListener (this);
            button.setOnClickListener (this);
            addView (button, lp);
        }
    }

    protected void onFocusChanged (boolean gainFocus, int direction,
Rect prevRect) {
        Log.v (TAG, "onFocusChanged"); <----- this function is not
called!!! why?
    }

    public void onFocusChange (View v, boolean hasFocus) {
        Log.v (TAG, "Button FocusChanged!");
    }

    public void onClick (View v) {
        Log.v (TAG, "Button Clicked!");
    }
}

--~--~---------~--~----~------------~-------~--~----~

2. canvas runtime error

Bitmap bitmaporg = BitmapFactory.decodeResource(getResources(),
R.drawable.a0 );
Bitmap bitmap_a = BitmapFactory.decodeResource(getResources(),
R.drawable.a1 );
Canvas canvas = new Canvas(bitmaporg);

.......
.......
.......

I have a runtime error at (Canvas canvas = new Canvas(bitmaporg).

why?

--~--~---------~--~----~------------~-------~--~----~

3. Android phone is more expensive than a 10.2" notebook computer

4. How to get the TouchPad or USB mouse working with Gitorious build?

5. How to get the TouchPad or USB mouse working with Gitorious build?

6. Is there way to invoke an IActivityManager.updateConfiguration() using public API of SDK?

7. Missing MapPoint in 1.5r2?