Handler写法
static class MyHandler extends Handler {
private final WeakReference<MyActivity> mActivity;
public MyHandler(MyActivity activity) {
mActivity = new WeakReference<MyActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
MyActivityactivity=mActivity.get();
switch (msg.what) {
activity.doSomething(); }}}
private final MyHandler handler = new IndexHandler(this);
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout’s root element)
LayoutInflater inflate = LayoutInflater.from(context);View view = inflate.inflate(R.layout.view_left_balloon, null);
//改正后
LayoutInflater inflate = LayoutInflater.from(context);View view = inflate.inflate(R.layout.view_left_balloon, new LinearLayout(context), false);
本文介绍了Android中Handler机制的实现方式,通过一个具体的例子展示了如何正确使用Handler进行消息处理。此外,还讨论了在使用LayoutInflater加载布局时避免传入null作为根视图的重要性,并给出了正确的实现方式。
503

被折叠的 条评论
为什么被折叠?



