"Couldn't add widget" in Fragment
I have problem with adding widget in Fragment. Here is some hardcoded adding 3 example widget:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View root = inflater.inflate(R.layout.fragment_widget, container, false);
mainlayout = (ViewGroup) root.findViewById(R.id.main_layout);
mAppWidgetManager = AppWidgetManager.getInstance(getContext());
mAppWidgetHost = new AppWidgetHost(getContext(), R.id.APPWIDGET_HOST_ID);
addButton = (Button) root.findViewById(R.id.bt_add);
removeButton = (Button) root.findViewById(R.id.bt_remove);
addButton.setOnClickListener(this);
removeButton.setOnClickListener(this);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(9);
Log.e(TAG, "createWidget: " + Integer.toString(9) + appWidgetInfo);
AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity(), 9, appWidgetInfo);
hostView.setAppWidget(9, appWidgetInfo);
mainlayout.addView(hostView);
appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(11);
Log.e(TAG, "createWidget: " + Integer.toString(11) + appWidgetInfo);
hostView = mAppWidgetHost.createView(getActivity(), 11, appWidgetInfo);
hostView.setAppWidget(11, appWidgetInfo);
mainlayout.addView(hostView);
appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(10);
Log.e(TAG, "createWidget: " + Integer.toString(10) + appWidgetInfo);
hostView = mAppWidgetHost.createView(getActivity(), 10, appWidgetInfo);
hostView.setAppWidget(10, appWidgetInfo);
mainlayout.addView(hostView);
mAppWidgetHost.startListening();
return root;
}
First is OK, second should be weather widget but it show only "Couldn't add widget" and last one is not loading emails.
When I do this in Activity everything works OK. What should I do to make it work in Fragment?
链接地址: http://www.djcxy.com/p/81416.html上一篇: Java Android Fragment ListView
下一篇: “无法添加小部件”在Fragment中