2011-02-14 90 views
0

我的活動每當我回來或主頁按鈕時崩潰,它發生在調用onPause方法時。我認爲它必須對光標執行一些操作,但是它不應該在應用程序管理它的時候執行。Android NPE當活動進入後臺時

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.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); 



    } 
} 
} 
+1

你可以張貼堆棧跟蹤 – ant 2011-02-14 14:03:35

回答

相關問題