所以我在我的應用程序中有一個pdf插件,它使用了公共類SimpleDocumentReader。這僅僅是一堂課,而不是一項活動。我在那裏時,它已經完成加載,你可以改變文件頁面的方法:在方法中獲取SharedPreferences的上下文
@Override
public void onLoadFinish(DocumentState.OPEN state) {
if (state == OPEN.SUCCESS) {
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences();
String page = pref.getString("example_list","");
goToPage(Integer.parseInt(page));
}
}
正如你所看到的,我想訪問defaultsharedpreferences,其中一個字符串存儲與我可以更改頁面。問題是,在getDeaultSharedPreferences()的括號中;我需要上下文。我已經試過幾件事情已經:
getApplicationContext()(沒有工作),
getActivity()也是非常愚蠢的。
還試圖使該方法收到這樣的背景下:
public void onLoadFinish(DocumentState.OPEN state, Context context) {
// line for getting sharedpreferences Context:
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
}
但它拋出幾個錯誤: 首先,@覆蓋不工作,因爲
method does not override method from its superclass
第二個錯誤在頂部,該類本身:
public class SimpleDocumentReader implements ReaderListener {}
引發此錯誤:
類「SimpleDocumentReader」必須聲明爲抽象或在ReaderListener中實現抽象方法'onLoadFinish(OPEN)'。
這是什麼意思?
編輯:這裏是如何的SimpleDocumentReader類正在啓動:
首先,在我的活動SimpleReaderFactory創建SimpleViewer:
SimpleDocumentReader v = SimpleReaderFactory.createSimpleViewer(this, null);
v.openUrl("myurl", "mypassword");
二,SimpleReaderFactory:
public class SimpleReaderFactory {
/**
* Creates and returns a PDF viewer used for viewing, reading, and
* navigating PDF document
*
* @param act
* The activity of the current application.
* @param listener
* The SimpleDocumentViewerListener instance.
*
* @return SimpleDocumentViewer The default PDF document viewer instance
*/
public static SimpleDocumentReader createSimpleViewer(Activity act,
SimpleDocumentReaderListener listener) {
SimpleDocumentReader viewer = new SimpleDocumentReader(act);
viewer.setListener(listener);
return viewer;
}
}
最後,我的SimpleDocumentReader:
public class SimpleDocumentReader implements ReaderListener {
// a lot of stuff here, I will post the important snippets of code:
public void onLoadFinish(DocumentState.OPEN state) {
if (state == OPEN.SUCCESS) {
mControlView.init(mAct);
RelativeLayout layout = new RelativeLayout(mAct);
layout.addView(mReaderView);
layout.addView(mControlView);
mAct.setContentView(layout);
CoordConverter.initCoordConverter(mAct, mReaderView);
SharedPreferences pref=
//my attempts at getting the defaultsharedpreferences
PreferenceManager.getDefaultSharedPreferences();
String hour = pref.getString("example_list","");
goToPage(Integer.parseInt(hour));
}
}
你在哪裏實現'LoaderManager.LoaderCallbacks'接口? – 2015-04-06 11:14:40
我的代碼中沒有找到任何接口(項目搜索),但是如果你想要的話,我可以粘貼SimpleDocumentReader類的整個代碼。 – 2015-04-06 11:18:51