2

Scenario:

SherlockFragmentActivity displaying SherlockFragment. SherlockFragment showing SherlockDialogFragment.

Verions:

  • Build API: Google APIs 15
  • Compatibility/Support Package: v4
  • ActionBarSherclock: 4.1.0
  • Android Emulator: API-8

Code:

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

For background jobs and data management, i'm using an IntentService. Therefore, i have created a helper class named 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);
    }
    ...
}

Part of the manifest:

    <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" />

EXCEPTION:

And this is the exception i'm getting, when clicking the save-button in the 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)

Thoughts:

What you can see in the exception is, that startActivityForResult() is called, instead of startActivity(). I think this COULD be a bug in the support.v4 package, but i'm not sure at all.

But what i can say is, that the NotificationServiceUtil class works fine from e.g. Activity and SherlockFragmentActivity.

Does anyone have an idea how to fix this?

stefanjunker
  • 367
  • 4
  • 13

2 Answers2

2

You're starting a Service, not an Activity. Change activity.startActivity() to activity.startService()

SeanPONeil
  • 3,901
  • 4
  • 29
  • 42
1

Just check the change, I have made to ur code, and don't forget to import

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);
}
...
}
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
  • see the part of the manifest, it's declared as a service, and the intent is directed to the class of the IntentSerivce, see NotificationServiceUtil – stefanjunker May 31 '12 at 14:34
  • u hav declared as "service" change it to – Chintan Raghwani May 31 '12 at 14:37
  • that makes no sense at all, since my IntentService will never be an Activity. But to prove that, i did what you suggested, and here's the exception: 05-31 16:51:11.535: E/AndroidRuntime(4127): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tsystems.res.itm.android/com.tsystems.res.itm.android.services.NotificationService}: java.lang.ClassCastException: com.tsystems.res.itm.android.services.NotificationService – stefanjunker May 31 '12 at 14:52
  • I have edited answer, just check it. n jus tell me is it work for u or not? – Chintan Raghwani May 31 '12 at 15:20
  • and also remove activity having "NotificationServic" from ur AndroidManifest.xml – Chintan Raghwani May 31 '12 at 15:21
  • comment on your edit: i don't see why i should use NotificationManager. i appreciate your help, but i really think you're going into the wrong direction. i'm not using android's NotificationManager, but a selfmade class called NotificationService, which extends IntentService. AFAIK i have to start an IntentService like i do in my helper class NotificationServiceUtil. have you realldy read through my complete question? – stefanjunker May 31 '12 at 15:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11994/discussion-between-stefanjunker-and-chintan-raghwani) – stefanjunker May 31 '12 at 15:26