开发者

Android NPE when activity goes to background

My activity crashes whenever I hit back or home button, it happens when calling onPause method. I think it has to do something with the cursor but it shouldn't since application manages it.

package mini.bookmarks.android.hr;

import java.io.ByteArrayInputStream;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable开发者_JAVA百科.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.Browser.BookmarkColumns;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class Main extends Activity {

private int FAVICON_SIZE = Constants.FAVICON_SIZE;
private int topMargin = Constants.topMargin;
private int leftMargin = Constants.leftMargin;

ScrollView scrollView;
BookmarkLayout bookmarkLayout;
RelativeLayout.LayoutParams params;
ImageView bookmark;

private float scale;

int bookmarkCounter;

Cursor cursor;

HashMap<Integer, String> urls = new HashMap<Integer, String>();

String [] projection = new String[] {
        BookmarkColumns.TITLE,
        BookmarkColumns.URL,
        BookmarkColumns.FAVICON,
};

String selection = BookmarkColumns.FAVICON + " IS NOT NULL";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    scrollView = (ScrollView) findViewById(R.id.ScrollView_main);
    bookmarkLayout = (BookmarkLayout) findViewById(R.id.BookmarkLayout_main);

    scale = this.getResources().getDisplayMetrics().density;
    loadResources();
}

private void loadResources() {

    this.startManagingCursor(cursor);
    cursor = managedQuery(Browser.BOOKMARKS_URI, projection, selection,
    null, Browser.BookmarkColumns.VISITS + " DESC");

    if(cursor.moveToFirst()) {
        bookmarkCounter = 0;
        ByteArrayInputStream blobImage;

        do{
           bookmark = new ImageView(this);
           bookmark.setId(bookmarkCounter++);
           bookmark.setBackgroundColor(Color.WHITE);

           blobImage = new ByteArrayInputStream(
                   cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

           bookmark.setImageDrawable(
                   Drawable.createFromStream(blobImage, "" + bookmark.getId()));

           urls.put(bookmark.getId(),
                   cursor.getString(
                            cursor.getColumnIndex(BookmarkColumns.URL)));

           bookmark.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent browserIntent = new Intent(
                            Intent.ACTION_VIEW, Uri.parse(urls.get(v.getId())));
                    startActivity(browserIntent);
                }
            });

           params = new RelativeLayout.LayoutParams(
                   (int) (scale * FAVICON_SIZE), (int) (scale * FAVICON_SIZE));

           params.topMargin = (int) (scale * topMargin);
           params.leftMargin = (int) (scale * leftMargin);
           bookmarkLayout.addView(bookmark, params);
        } while (cursor.moveToNext());
     }
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()) {
    case R.id.menu_item_settings:
        return true;

    case R.id.menu_item_about:
        return true;

    default:
        return super.onOptionsItemSelected(item);



    }
}
}


This issue report suggests that you are not supposed to pass a null cursor into startManagingCursor().

Also have a look at the answer to this question:

  • Why is my activity crashing when hitting the home button?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜