设想:
显示 SherlockFragment 的 SherlockFragmentActivity。SherlockFragment 显示 SherlockDialogFragment。
版本:
- 构建 API:谷歌 API 15
- 兼容性/支持包:v4
- ActionBarSherclock:4.1.0
- 安卓模拟器:API-8
代码:
SherlockFragment活动:
public class TrailerDetailSherlockFragmentActivity extends SherlockFragmentActivity {
...
/*
* set up the fragment which will display the sensor details,
* and allow changes of the alarm settings
*/
detailFragment = new TempsensorDetailSherlockFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.framelayout_td_sensordetail, detailFragment);
transaction.commit();
...
}
夏洛克片段:
public class TempsensorDetailSherlockFragment extends SherlockFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
// set the onClick method for the alarm values
View alarmValueLayout = tempsensorDetailView.findViewById(R.id.ll_sd_minmax);
alarmValueLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = activity.getSupportFragmentManager();
// Create and show the dialog.
SetAlarmTemperatureSherlockDialogFragment setAlarmTempsDialogFragment = new SetAlarmTemperatureSherlockDialogFragment(deviceString, alarm);
setAlarmTempsDialogFragment.show(fm, "dialog");
}
});
...
}
SherlockDialogFragment:
public class SetAlarmTemperatureSherlockDialogFragment extends SherlockDialogFragment {
...
@Override
public void onAttach(Activity activity) {
...
this.activity = (SherlockFragmentActivity) activity;
notificationServiceUtil = new NotificationServiceUtil(activity);
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
saveButton = (ImageButton) view.findViewById(R.id.btn_setalarmtemp_save);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
notificationServiceUtil.alarmSubscribe(deviceString, bundle, new AlarmSubscribeHandler());
}
});
...
}
对于后台作业和数据管理,我使用的是 IntentService。因此,我创建了一个名为 NotificationServiceUtil 的辅助类:
public class NotificationServiceUtil {
...
public NotificationServiceUtil(Activity activity) {
this.activity = activity;
}
private void _initRestApiServiceIntent (String action) {
restApiServiceIntent = new Intent(activity, NotificationService.class);
restApiServiceIntent.setAction(action);
}
public void alarmSubscribe(String deviceString, Bundle bundle, Handler handler) {
_initRestApiServiceIntent(NotificationService.ACTION_ALARM_SET);
restApiServiceIntent.putExtras(bundle);
restApiServiceIntent.putExtra(NotificationService.EXTRAKEY_DEVICE_STRING, deviceString);
restApiServiceIntent.putExtra(NotificationService.EXTRAKEY_MESSENGER, new Messenger(handler));
activity.startActivity(restApiServiceIntent);
}
...
}
清单的一部分:
<activity
android:name=".activitys.MainSherlockFragmentActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activitys.TrailerDetailSherlockFragmentActivity"
android:label="@string/app_name"/>
<service android:name=".services.NotificationService" />
例外:
这是我得到的例外,当单击 SetAlarmTemperatureSherlockDialogFragment 中的保存按钮时:
05-31 15:29:34.084: E/AndroidRuntime(4041): FATAL EXCEPTION: main
05-31 15:29:34.084: E/AndroidRuntime(4041): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.tsystems.res.itm.android/com.tsystems.res.itm.android.services.NotificationService}; have you declared this activity in your AndroidManifest.xml?
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.app.Activity.startActivityForResult(Activity.java:2817)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:674)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.app.Activity.startActivity(Activity.java:2923)
05-31 15:29:34.084: E/AndroidRuntime(4041): at com.tsystems.res.itm.android.util.NotificationServiceUtil.alarmSubscribe(NotificationServiceUtil.java:121)
05-31 15:29:34.084: E/AndroidRuntime(4041): at com.tsystems.res.itm.android.dialogs.SetAlarmTemperatureSherlockDialogFragment$1.onClick(SetAlarmTemperatureSherlockDialogFragment.java:113)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.view.View.performClick(View.java:2408)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.view.View$PerformClick.run(View.java:8816)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.os.Handler.handleCallback(Handler.java:587)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.os.Looper.loop(Looper.java:123)
05-31 15:29:34.084: E/AndroidRuntime(4041): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 15:29:34.084: E/AndroidRuntime(4041): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 15:29:34.084: E/AndroidRuntime(4041): at java.lang.reflect.Method.invoke(Method.java:521)
05-31 15:29:34.084: E/AndroidRuntime(4041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 15:29:34.084: E/AndroidRuntime(4041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 15:29:34.084: E/AndroidRuntime(4041): at dalvik.system.NativeStart.main(Native Method)
想法:
您可以在异常中看到,调用的是 startActivityForResult(),而不是 startActivity()。我认为这可能是 support.v4 包中的一个错误,但我完全不确定。
但我能说的是,NotificationServiceUtil 类在 Activity 和 SherlockFragmentActivity 中运行良好。
有谁知道如何解决这个问题?