每当我回击或主页按钮时,我的活动都会崩溃,当调用onPause方法时会发生这种情况。我认为它必须对光标做一些事情,但它不应该由于应用程序管理它。
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);
}
}
}