我想將WidgetHostActivity轉換爲Fragment,但當我試圖打開WidgetManager時出現錯誤。這裏是轉換到目前爲止,我已經做了(沒有的OnStart和調用OnStop)...WidgetHost Activity to Fragment
public class FragmentBlue extends Fragment implements OnLongClickListener
{
static final String TAG = "FragmentBlue";
public static final int RESULT_OK = -1;
public static final int RESULT_CANCELED = 0;
AppWidgetManager mAppWidgetManager;
AppWidgetHost mAppWidgetHost;
ViewGroup mainlayout;
TextView text;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blue, container, false);
mainlayout = (ViewGroup) view.findViewById(R.id.main_layout);
text = (TextView)view.findViewById(R.id.tvtext);
mainlayout.setOnLongClickListener(this);
mAppWidgetManager = AppWidgetManager.getInstance(getActivity());
mAppWidgetHost = new AppWidgetHost(getActivity(), R.id.APPWIDGET_HOST_ID);
return view;
}
/**
* Launches the menu to select the widget. The selected widget will be on
* the result of the activity.
*/
void selectWidget() {
text.setText(null);
int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
addEmptyData(pickIntent);
startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
}
/**
* This avoids a bug in the com.android.settings.AppWidgetPickActivity,
* which is used to select widgets. This just adds empty extras to the
* intent, avoiding the bug.
*
* See more: http://code.google.com/p/android/issues/detail?id=4272
*/
void addEmptyData(Intent pickIntent) {
ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
}
/**
* If the user has selected an widget, the result will be in the 'data' when
* this function is called.
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
configureWidget(data);
} else if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
createWidget(data);
}
} else if (resultCode == RESULT_CANCELED && data != null) {
int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
/**
* Checks if the widget needs any configuration. If it needs, launches the
* configuration activity.
*/
private void configureWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
if (appWidgetInfo.configure != null) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(appWidgetInfo.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
} else {
createWidget(data);
}
}
/**
* Creates the widget and adds to our view layout.
*/
public void createWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity(), appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
mainlayout.addView(hostView);
Log.i(TAG, "The widget size is: " + appWidgetInfo.minWidth + "*" + appWidgetInfo.minHeight);
}
我認爲這個問題是到startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
因爲當我改變R.id.REQUEST_PICK_APPWIDGET
的數字(例如1)WidgetManager打開,當我選擇一個已安裝的小部件它什麼都不做(只是關閉窗口)。 我希望這是可以理解的。
的誤差修改我得到的是:
06-08 16:41:00.896: E/AndroidRuntime(1285): FATAL EXCEPTION: main
06-08 16:41:00.896: E/AndroidRuntime(1285): Process: pl.looksok.viewpagerdemo, PID: 1285
06-08 16:41:00.896: E/AndroidRuntime(1285): java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:852)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:889)
06-08 16:41:00.896: E/AndroidRuntime(1285): at pl.looksok.viewpagerdemo.FragmentBlue.selectWidget(FragmentBlue.java:72)
06-08 16:41:00.896: E/AndroidRuntime(1285): at pl.looksok.viewpagerdemo.FragmentBlue.onLongClick(FragmentBlue.java:214)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.view.View.performLongClick(View.java:4474)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.view.View$CheckForLongPress.run(View.java:18401)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.os.Handler.handleCallback(Handler.java:733)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.os.Handler.dispatchMessage(Handler.java:95)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.os.Looper.loop(Looper.java:136)
06-08 16:41:00.896: E/AndroidRuntime(1285): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 16:41:00.896: E/AndroidRuntime(1285): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:41:00.896: E/AndroidRuntime(1285): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 16:41:00.896: E/AndroidRuntime(1285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 16:41:00.896: E/AndroidRuntime(1285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 16:41:00.896: E/AndroidRuntime(1285): at dalvik.system.NativeStart.main(Native Method)
弄來哪些錯誤? – eleven
我在主帖子中添加了錯誤 – Tzeik