user1497684
user1497684

Reputation:

Getting Null Pointer Exception at onResume() in activity in android

I had a customview.when the app launches for the first time i am getting null pointer in the particular activity because of null pointer exception in onResume() Activity..

The code i am using is

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
            ....................
             ..................
                ..........
    int index = 0;
    if (getLastNonConfigurationInstance() != null) {
        index = (Integer) getLastNonConfigurationInstance();
    }
            mCurlView = (CurlView) findViewById(R.id.curl);
            mCurlView.setPageProvider(new PageProvider());
            mCurlView.setSizeChangedObserver(new SizeChangedObserver());
            mCurlView.setCurrentIndex(index);
            mCurlView.setBackgroundColor(0xFF202830);
}

 public static Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width,
                v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
        v.draw(c);
        return b;
    }
@Override
public void onPause() {
    super.onPause();
    mCurlView.onPause();
}
@Override
public void onResume() {
    super.onResume();
    mCurlView.onResume();
}

I am getting null pointer exception at this line mCurlView.onResume(); i.e for the first time of the app launch only!! How can i resolve this issue?

Upvotes: 2

Views: 1973

Answers (2)

pvn
pvn

Reputation: 2126

This may be happening because member variable mCurView might be null. You must check that before you use it. In the onResume() use :

if(mCurlView != null)
   mCurlView.onResume();

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128438

You must check NULL condition:

@Override
public void onResume() {
    super.onResume();

    if(mCurlView!=null)
       mCurlView.onResume();
}

Upvotes: 0

Related Questions