标题 : 不作死就不会死!!!!!!
类 : ActivityA,ActivityB,BookListAdapter,Book,HttpManager
测试顺序 : ActivityA >> ActivityB >> ActivityA(ActivityB"销毁") >> ActivityB
嘿嘿!!!! 出现如下:
Bug :
每次点击按钮都是从异步获取数据显示到listview中,运行时第一次点击没问题,数据获取并都显示正常,但是这个listview所在的ActivityB关闭掉后,重新点击按钮时,listview竟然没任何数据了 ...WTF...
解决方法:
1.(渣渣方案)把HttpManager 中 asyncBooks方法中的
if (mListener == null) {
mListener = listener;
}
改为
mListener = listener;
2.把 HttpManager 中的方法改为非static的,在ActivityB中直接new HttpManager()一个对象:
public class ActivityB extends AppCompatActivity implements HttpManager.LoadBookListener {
......
HttpManager httpManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
......
httpManager = new HttpManager();
}
@Override
protected void onResume() {
......
httpManager.asyncBooks(this);
}
......
}
总结: 如非必要,不要使用static做方法及参数的修饰符!!!!! 会引起一些不为人知的内存泄漏
ActivityA
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_A);
}
public void onClick(View view) {
startActivity(new Intent(this, ActivityB.class));
}
}
ActivityB
public class ActivityB extends AppCompatActivity implements HttpManager.LoadBookListener {
private BookListAdapter adapter;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_B);
initView();
}
private void initView() {
listView = (ListView) findViewById(R.id.listView);
adapter = new BookListAdapter();
listView.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
HttpManager.asyncBooks(this);
}
@Override
public void onLoadBooksEnd(final List<Book> books) {
adapter.addAll(books, true);
}
}
BookListAdapter
class BookListAdapter extends MyBaseAdapter<Book> {
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_book_list, viewGroup, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}Book book = getItem(i);
viewHolder.imgBook.setImageResource(R.mipmap.ic_launcher);
viewHolder.tvBookName.setText(book.getTitle());
viewHolder.tvBookSub.setText(book.getSub());
viewHolder.tvBookAuthor.setText(book.getAuthor());
viewHolder.tvBookReading.setText(book.getReading());
return view;
}
private static class ViewHolder {
ImageView imgBook;
TextView tvBookName;
TextView tvBookSub;
TextView tvBookAuthor;
TextView tvBookReading;
ViewHolder(View view) {
do....findViewById...
}
}
}
Book
public class Book implements Serializable{
private int id;
private String img;//封面
private int img1;
private String title;//书名
private String sub;//简介
private String author;
private String reading;//阅读人数
private String online;
private String bytime;//上线时间
do...get...
do...set...
}
HttpManager
class HttpManager {
private static LoadBookListener mListener;
interface LoadBookListener {
void onLoadBooksEnd(List<Book> books);
}
static void asyncBooks(LoadBookListener listener) {
//罪魁祸首在这!!!!!!!
if (mListener == null) {
mListener = listener;
}
AsyncBooksTask task = new AsyncBooksTask();
task.execute();
}
private static class AsyncBooksTask extends AsyncTask<Void, Void, List<Book>> {
@Override
protected List<Book> doInBackground(Void... params) {
List<Book> books = new ArrayList<>();
return getBooks(books);
}
@Override
protected void onPostExecute(List<Book> books) {
listener.onLoadBooksEnd(books);
}
}
private static List<Book> getBooks(List<Book> books) {
for (int i = 0; i < 12; i++) {
String title = "title" + i;
String author = "author" + i;
String sub = "sub" + i;
String img = "img" + i;
String reading = "reading" + i;
String online = "online" + i;
String byTime = "byTime" + i;
Book book = new Book(i + 1, img, title, sub, author, reading, online, byTime);
books.add(book);
}
return books;
}
}