0
我目前正在開發一個Android應用程序,並且正在使用序列化文件。我有一個方法將一個Java對象序列化爲一個帶有Context參數的文件。在另一個類中工作時,我可以檢索有效的Context對象,但是當我在擴展AppCompatActivity的這個類上時,我不太確定該怎麼做。我嘗試使用getApplicationContext(),但仍然給我一個空值的上下文。在AppCompatActivity中使用上下文
這裏是我到目前爲止基地:
public class BookView extends AppCompatActivity {
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
Book book = Browse.books.get(getIntent().getIntExtra("index", 0));
Browse.book = book;
setUpScreen(book);
context = getApplicationContext();
}
private void setUpChapters(Book book){
ListView chapters = (ListView) findViewById(R.id.chapters);
ChapterAdapter adapter = new ChapterAdapter(this, R.layout.row, book.getChapters());
chapters.setAdapter(adapter);
if (context != null) {
book.serialize(context);
}
else {
System.out.println("Null bookview context");
}
chapters.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), Reader.class);
myIntent.putExtra("index", position);
startActivity(myIntent);
}
});
}
}
如何獲得背景下,我能夠進入連載一個非空值?
的http://stackoverflow.com/a/3572553/4357238 – Edward