我正在使用具有选项卡滑动的启动器进行工作,除了出现此错误的问题之外,其他所有操作都有效:
Type mismatch: cannot convert from ShelvesActivity to Fragment
所以这是我的代码:
MainFrag.java
package it.gmariotti.android.examples.googleaccount;
import org.curiouscreature.android.shelves.activity.ShelvesActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import com.d4a.tobias.R;
public class MainFrag extends FragmentActivity{
private MyAdapter mAdapter;
private ViewPager mPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainfrag);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
String arr[]={"Calculator","GoogleDrive","Books", "QuickLaunch"};
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return arr[position];
}
@Override
public int getCount() {
return arr.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Calculator();
case 1:
return new MainActivity();
case 2:
return new ShelvesActivity();
case 3:
return new QuickLaunch();
default:
return null;
}
}
}
}
这是另一个
ShelvesActivity.java
package org.curiouscreature.android.shelves.activity;
import android.support.v4.app.Fragment;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Config;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewStub;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.PopupWindow;
import org.curiouscreature.android.shelves.drawable.FastBitmapDrawable;
import org.curiouscreature.android.shelves.drawable.CrossFadeDrawable;
import org.curiouscreature.android.shelves.provider.BooksStore;
import org.curiouscreature.android.shelves.provider.BookStoreFactory;
import org.curiouscreature.android.shelves.provider.BooksManager;
import org.curiouscreature.android.shelves.provider.BooksUpdater;
import org.curiouscreature.android.shelves.scan.ScanIntent;
import org.curiouscreature.android.shelves.util.ImageUtilities;
import org.curiouscreature.android.shelves.util.ImportUtilities;
import org.curiouscreature.android.shelves.util.UserTask;
import org.curiouscreature.android.shelves.util.UIUtilities;
import org.curiouscreature.android.shelves.view.ShelvesView;
import org.curiouscreature.android.shelves.R;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
public class ShelvesActivity extends FragmentActivity {
private static final String LOG_TAG = "Shelves";
private static final int REQUEST_SCAN_FOR_ADD = 1;
private static final int REQUEST_SCAN_FOR_CHECK = 2;
private static final int COVER_TRANSITION_DURATION = 175;
private static final int MESSAGE_UPDATE_BOOK_COVERS = 1;
private static final int DELAY_SHOW_BOOK_COVERS = 550;
private static final int WINDOW_DISMISS_DELAY = 600;
private static final int WINDOW_SHOW_DELAY = 600;
private static final String ACTION_IMPORT = "shelves.intent.action.ACTION_IMPORT";
private static final String STATE_IMPORT_IN_PROGRESS = "shelves.import.inprogress";
private static final String STATE_IMPORT_BOOKS = "shelves.import.books";
private static final String STATE_IMPORT_INDEX = "shelves.import.index";
private static final String STATE_ADD_IN_PROGRESS = "shelves.add.inprogress";
private static final String STATE_ADD_BOOK = "shelves.add.book";
private ImportTask mImportTask;
private AddTask mAddTask;
private BooksUpdater mBooksUpdater;
private final Handler mScrollHandler = new ScrollHandler();
private int mScrollState = ShelvesScrollManager.SCROLL_STATE_IDLE;
private boolean mPendingCoversUpdate;
private boolean mFingerUp = true;
private PopupWindow mPopup;
private FastBitmapDrawable mDefaultCover;
private View mGridPosition;
private TextView mGridPositionText;
private ProgressBar mImportProgress;
private View mImportPanel;
private View mAddPanel;
private ShelvesView mGrid;
private Bundle mSavedState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_shelves);
getWindow().setBackgroundDrawable(null);
mBooksUpdater = new BooksUpdater(this);
setupViews();
handleSearchQuery(getIntent());
}
int getScrollState() {
return mScrollState;
}
boolean isPendingCoversUpdate() {
return mPendingCoversUpdate;
}
FastBitmapDrawable getDefaultCover() {
return mDefaultCover;
}
private void handleSearchQuery(Intent queryIntent) {
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
onSearch(queryIntent);
} else if (Intent.ACTION_VIEW.equals(queryAction)) {
final Intent viewIntent = new Intent(Intent.ACTION_VIEW, queryIntent.getData());
startActivity(viewIntent);
}
}
private void onSearch(Intent intent) {
final String queryString = intent.getStringExtra(SearchManager.QUERY);
mGrid.setFilterText(queryString);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
final String action = intent.getAction();
if (Intent.ACTION_SEARCH.equals(action)) {
onSearch(intent);
} else if (Intent.ACTION_VIEW.equals(action)) {
final Intent viewIntent = new Intent(Intent.ACTION_VIEW, intent.getData());
startActivity(viewIntent);
} else if (ACTION_IMPORT.equals(action)) {
onImport();
}
}
private void setupViews() {
final BooksAdapter adapter = new BooksAdapter(this);
mDefaultCover = adapter.getDefaultCover();
mGrid = (ShelvesView) findViewById(R.id.grid_shelves);
final ShelvesView grid = mGrid;
grid.setTextFilterEnabled(true);
grid.setAdapter(adapter);
grid.setOnScrollListener(new ShelvesScrollManager());
grid.setOnTouchListener(new FingerTracker());
grid.setOnItemSelectedListener(new SelectionTracker());
grid.setOnItemClickListener(new BookViewer());
registerForContextMenu(grid);
mGridPosition = getLayoutInflater().inflate(R.layout.grid_position, null);
mGridPositionText = (TextView) mGridPosition.findViewById(R.id.text);
}
@Override
protected void onResume() {
super.onResume();
mBooksUpdater.start();
if (mSavedState != null) restoreLocalState(mSavedState);
}
@Override
protected void onPause() {
super.onPause();
stopBooksUpdater();
}
@Override
protected void onStop() {
super.onStop();
stopBooksUpdater();
}
@Override
protected void onDestroy() {
super.onDestroy();
dismissPopup();
stopBooksUpdater();
onCancelAdd();
onCancelImport();
ImageUtilities.cleanupCache();
}
private void stopBooksUpdater() {
final BooksUpdater booksUpdater = mBooksUpdater;
booksUpdater.clear();
booksUpdater.stop();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
restoreLocalState(savedInstanceState);
mSavedState = null;
}
private void restoreLocalState(Bundle savedInstanceState) {
restoreAddTask(savedInstanceState);
restoreImportTask(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveAddTask(outState);
saveImportTask(outState);
mSavedState = outState;
}
private void saveAddTask(Bundle outState) {
final AddTask task = mAddTask;
if (task != null && task.getStatus() != UserTask.Status.FINISHED) {
final String bookId = task.getBookId();
task.cancel(true);
if (bookId != null) {
outState.putBoolean(STATE_ADD_IN_PROGRESS, true);
outState.putString(STATE_ADD_BOOK, bookId);
}
mAddTask = null;
}
}
private void restoreAddTask(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(STATE_ADD_IN_PROGRESS)) {
final String id = savedInstanceState.getString(STATE_ADD_BOOK);
if (!BooksManager.bookExists(getContentResolver(), id)) {
mAddTask = (AddTask) new AddTask().execute(id);
}
}
}
private void saveImportTask(Bundle outState) {
final ImportTask task = mImportTask;
if (task != null && task.getStatus() != UserTask.Status.FINISHED) {
task.cancel(true);
outState.putBoolean(STATE_IMPORT_IN_PROGRESS, true);
outState.putStringArrayList(STATE_IMPORT_BOOKS, task.mBooks);
outState.putInt(STATE_IMPORT_INDEX, task.mImportCount.get());
mImportTask = null;
}
}
private void restoreImportTask(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(STATE_IMPORT_IN_PROGRESS)) {
ArrayList<String> books = savedInstanceState.getStringArrayList(STATE_IMPORT_BOOKS);
int index = savedInstanceState.getInt(STATE_IMPORT_INDEX);
if (books != null) {
if (index < books.size()) {
mImportTask = (ImportTask) new ImportTask(books, index).execute();
}
} else {
mImportTask = (ImportTask) new ImportTask().execute();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shelves, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_add_search:
onAddSearch();
return true;
case R.id.menu_item_add:
onAdd();
return true;
case R.id.menu_item_check:
onCheck();
return true;
case R.id.menu_item_search:
onSearchRequested();
return true;
case R.id.menu_item_settings:
onSettings();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void onSettings() {
SettingsActivity.show(this);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final boolean scanAvailable = ScanIntent.isInstalled(this);
MenuItem item;
item = menu.findItem(R.id.menu_item_add);
item.setEnabled(scanAvailable && (mAddTask == null ||
mAddTask.getStatus() == UserTask.Status.FINISHED));
item = menu.findItem(R.id.menu_item_check);
item.setEnabled(scanAvailable);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return onSearchRequested();
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(((TextView) info.targetView).getText());
getMenuInflater().inflate(R.menu.book, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)
item.getMenuInfo();
final BookViewHolder holder = (BookViewHolder) info.targetView.getTag();
switch (item.getItemId()) {
case R.id.context_menu_item_view:
onView(holder.bookId);
return true;
case R.id.context_menu_item_buy:
onBuy(BooksManager.findBook(getContentResolver(), holder.bookId));
return true;
case R.id.context_menu_item_delete:
onDelete(holder.bookId);
return true;
}
return super.onContextItemSelected(item);
}
private void onView(String bookId) {
BookDetailsActivity.show(this, bookId);
}
private void onBuy(BooksStore.Book book) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(book.getDetailsUrl()));
startActivity(intent);
}
private void onDelete(String bookId) {
if (BooksManager.deleteBook(getContentResolver(), bookId)) {
UIUtilities.showToast(this, R.string.success_book_deleted);
}
}
private void startScan(int code) {
try {
final Intent intent = new Intent(ScanIntent.INTENT_ACTION_SCAN);
intent.putExtra(ScanIntent.INTENT_EXTRA_SCAN_MODE,
ScanIntent.INTENT_EXTRA_PRODUCT_MODE);
startActivityForResult(intent, code);
} catch (ActivityNotFoundException e) {
UIUtilities.showToast(this, R.string.error_missing_barcode_scanner, true);
}
}
private void onAddSearch() {
AddBookActivity.show(this);
}
private void onAdd() {
startScan(REQUEST_SCAN_FOR_ADD);
}
private void onCheck() {
startScan(REQUEST_SCAN_FOR_CHECK);
}
private void onImport() {
if (mImportTask == null || mImportTask.getStatus() == ImportTask.Status.FINISHED) {
mImportTask = (ImportTask) new ImportTask().execute();
} else {
UIUtilities.showToast(this, R.string.error_import_in_progress);
}
}
private void onCancelAdd() {
if (mAddTask != null && mAddTask.getStatus() == UserTask.Status.RUNNING) {
mAddTask.cancel(true);
mAddTask = null;
}
}
private void onCancelImport() {
if (mImportTask != null && mImportTask.getStatus() == UserTask.Status.RUNNING) {
mImportTask.cancel(true);
mImportTask = null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_SCAN_FOR_ADD:
onScanAdd(data);
break;
case REQUEST_SCAN_FOR_CHECK:
onScanCheck(data);
break;
}
}
}
private void onScanAdd(Intent data) {
final Bundle bundle = data.getExtras();
if (ScanIntent.FORMAT_EAN_13.equals(bundle.getString(ScanIntent.SCAN_RESULT_FORMAT))) {
final String id = bundle.getString(ScanIntent.SCAN_RESULT);
if (!BooksManager.bookExists(getContentResolver(), id)) {
mAddTask = (AddTask) new AddTask().execute(id);
} else {
UIUtilities.showToast(this, R.string.error_book_exists);
}
}
}
private void onScanCheck(Intent data) {
final Bundle bundle = data.getExtras();
if (ScanIntent.FORMAT_EAN_13.equals(bundle.getString(ScanIntent.SCAN_RESULT_FORMAT))) {
final String id = bundle.getString(ScanIntent.SCAN_RESULT);
final String bookId = BooksManager.findBookId(getContentResolver(), id);
if (bookId == null) {
UIUtilities.showImageToast(this, R.string.success_book_not_found,
getResources().getDrawable(R.drawable.unknown_book));
} else {
UIUtilities.showImageToast(this, R.string.error_book_exists,
ImageUtilities.getCachedCover(bookId, mDefaultCover));
}
}
}
private void showPanel(View panel, boolean slideUp) {
panel.startAnimation(AnimationUtils.loadAnimation(this,
slideUp ? R.anim.slide_in : R.anim.slide_out_top));
panel.setVisibility(View.VISIBLE);
}
private void hidePanel(View panel, boolean slideDown) {
panel.startAnimation(AnimationUtils.loadAnimation(this,
slideDown ? R.anim.slide_out : R.anim.slide_in_top));
panel.setVisibility(View.GONE);
}
private void updateBookCovers() {
mPendingCoversUpdate = false;
final ShelvesView grid = mGrid;
final FastBitmapDrawable cover = mDefaultCover;
final int count = grid.getChildCount();
for (int i = 0; i < count; i++) {
final View view = grid.getChildAt(i);
final BookViewHolder holder = (BookViewHolder) view.getTag();
if (holder.queryCover) {
final String bookId = holder.bookId;
FastBitmapDrawable cached = ImageUtilities.getCachedCover(bookId, cover);
CrossFadeDrawable d = holder.transition;
d.setEnd(cached.getBitmap());
holder.title.setCompoundDrawablesWithIntrinsicBounds(null, null,
null, d);
d.startTransition(COVER_TRANSITION_DURATION);
holder.queryCover = false;
}
}
grid.invalidate();
}
private void postUpdateBookCovers() {
Handler handler = mScrollHandler;
Message message = handler.obtainMessage(MESSAGE_UPDATE_BOOK_COVERS, ShelvesActivity.this);
handler.removeMessages(MESSAGE_UPDATE_BOOK_COVERS);
mPendingCoversUpdate = true;
handler.sendMessage(message);
}
private void dismissPopup() {
if (mPopup != null) {
mPopup.dismiss();
}
}
private void showPopup() {
if (mPopup == null) {
PopupWindow p = new PopupWindow(this);
p.setFocusable(false);
p.setContentView(mGridPosition);
p.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
p.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
p.setBackgroundDrawable(null);
p.setAnimationStyle(R.style.PopupAnimation);
mPopup = p;
}
if (mGrid.getWindowVisibility() == View.VISIBLE) {
mPopup.showAtLocation(mGrid, Gravity.CENTER, 0, 0);
}
}
private class AddTask extends UserTask<String, Void, BooksStore.Book> {
private final Object mLock = new Object();
private String mBookId;
@Override
public void onPreExecute() {
if (mAddPanel == null) {
mAddPanel = ((ViewStub) findViewById(R.id.stub_add)).inflate();
((ProgressBar) mAddPanel.findViewById(R.id.progress)).setIndeterminate(true);
((TextView) mAddPanel.findViewById(R.id.label_import)).setText(
getText(R.string.add_label));
final View cancelButton = mAddPanel.findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onCancelAdd();
}
});
}
showPanel(mAddPanel, false);
}
String getBookId() {
synchronized (mLock) {
return mBookId;
}
}
public BooksStore.Book doInBackground(String... params) {
synchronized (mLock) {
mBookId = params[0];
}
return BooksManager.loadAndAddBook(getContentResolver(), mBookId,
BookStoreFactory.get(ShelvesActivity.this));
}
@Override
public void onCancelled() {
hidePanel(mAddPanel, false);
}
@Override
public void onPostExecute(BooksStore.Book book) {
if (book == null) {
UIUtilities.showToast(ShelvesActivity.this, R.string.error_adding_book);
} else {
UIUtilities.showFormattedImageToast(ShelvesActivity.this, R.string.success_added,
ImageUtilities.getCachedCover(book.getInternalId(), mDefaultCover),
book.getTitle());
}
hidePanel(mAddPanel, false);
}
}
private class ImportTask extends UserTask<Void, Integer, Integer> {
private ContentResolver mResolver;
final AtomicInteger mImportCount = new AtomicInteger();
ArrayList<String> mBooks;
ImportTask() {
}
ImportTask(ArrayList<String> books, int index) {
mBooks = books;
mImportCount.set(index);
}
@Override
public void onPreExecute() {
if (mImportPanel == null) {
mImportPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
mImportProgress = (ProgressBar) mImportPanel.findViewById(R.id.progress);
final View cancelButton = mImportPanel.findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onCancelImport();
}
});
}
mResolver = getContentResolver();
mImportProgress.setProgress(0);
showPanel(mImportPanel, true);
还有更多代码,但超出了我可以发布的字数上限!!
我将其从
Activity
更改为FragmentActivity
我究竟做错了什么?
最佳答案
您的问题似乎在这里。
public Fragment getItem(int position) {
switch (position) {
//...
case 2:
return new ShelvesActivity();
//...
该方法必须返回
Fragment
对象或从Fragment
继承的类的对象。 ShelvesActivity
不满足这些要求。