在(Sherlock)DialogFragment中调用startActivity(intentServiceIntent)会导致ActivityNotFoundException

时间:2012-05-31 14:21:56

标签: android android-intent actionbarsherlock android-dialogfragment android-compatibility

情境:

显示SherlockFragment的SherlockFragmentActivity。 SherlockFragment展示了SherlockDialogFragment。

Verions:

  • 构建API:Google API 15
  • 兼容性/支持包:v4
  • ActionBarSherclock:4.1.0
  • Android模拟器:API-8

代码:

SherlockFragmentActivity:

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();   
    ...
    }

SherlockFragment:

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类可以正常工作。活动和SherlockFragmentActivity。

有没有人知道如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您正在启动服务,而非活动。将activity.startActivity()更改为activity.startService()

答案 1 :(得分:1)

只需检查更改,我已对您的代码进行了修改, 并且不要忘记导入

import android.app.NotificationManager;    //    in ur activity

public class NotificationServiceUtil {
    ...
    public NotificationServiceUtil(Activity activity) {
        this.activity = activity;
    }

    private void _initRestApiServiceIntent (String action) {
        NotificationManager nm = (NotificationManager) getSystemService(activity.NOTIFICATION_SERVICE);  <------ CHANGE
        restApiServiceIntent = new Intent(activity, nm.getClass());  <--- CHANGE
        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);
}
...
}