【问题标题】:Getting java.lang.IllegalStateException when I try to use getSystemService in library project当我尝试在库项目中使用 getSystemService 时出现 java.lang.IllegalStateException
【发布时间】:2013-12-31 04:10:49
【问题描述】:

我正在尝试构建一个库,只要包含在应用程序项目中,它就会在系统托盘上弹出persistent notification。我制作了一个正常的应用程序并在运行良好的设备上对其进行了测试。现在,我已经将该项目转换为库并将该库引用到 android 项目。

这是我的MainActivity,它被命名为库项目的SearchBar

public class SearchBar extends Activity {

NotificationManager nm;
public static String PACKAGE_NAME;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_bar);

    PACKAGE_NAME = getApplicationContext().getPackageName();
}

public void searchBar() {

    Notification notification = new Notification(R.drawable.ic_stat_notify,
            "STest", System.currentTimeMillis());

    RemoteViews contentView = new RemoteViews(PACKAGE_NAME,
            R.layout.persistent_notification_layout);

    notification.contentView = contentView;

    Intent notificationIntent = new Intent();
    notificationIntent.setClassName("com.example.searchbar",
            "com.example.searchbar.SearchAutoSuggest");

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    nm = (NotificationManager) getSystemService(SearchBar.NOTIFICATION_SERVICE);

    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    PendingIntent contentIntent = PendingIntent.getActivity(this, 2,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.contentIntent = contentIntent;

    nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);
}

}

而且,我将这个库调用到我的 testProject's MainActivity 中,如下所示:

public class MainActivity extends Activity {

SearchBar notification;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notification = new SearchBar();

    notification.searchBar();

}
}

现在,当我运行我的 testProject 时,我收到错误消息:

 Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4463)
at com.example.searchbar.SearchBar.searchBar(SearchBar.java:43)
at com.example.libtest.MainActivity.onCreate(MainActivity.java:19)

开启:

nm = (NotificationManager) getSystemService(SearchBar.NOTIFICATION_SERVICE);

我尝试/搜索了几个东西,但没有一个能帮助我解决问题。 我究竟做错了什么?任何形式的帮助将不胜感激。

【问题讨论】:

    标签: java android eclipse android-library


    【解决方案1】:

    IllegalStateException:系统服务不可用于活动 在 onCreate() 之前

    因为您在 SearchBar 类中扩展了 Activity,但没有在 AndroidManifest.xml 中注册。

    所以创建SearchBar类作为普通java类而不扩展Activity并使用类构造函数或作为方法参数将MainActivity上下文传递给SearchBar类:

    public class SearchBar {
    
    NotificationManager nm;
    public static String PACKAGE_NAME;
    Context context;
     public  SearchBar(Context context) {
        this.context=context;
        PACKAGE_NAME = context.getPackageName();
    }
    ....
    

    【讨论】:

    • 非常感谢。你能再告诉我一件事吗?当用户点击通知时,我必须通过Intentactivity 类在我的库中,为此我只需在 testProject's 清单文件中添加活动。那个?接受你的回答。
    • @Anupam :是的,只需在 testProject 的清单文件中声明要从库项目调用的活动
    【解决方案2】:

    Activity 并不意味着通过 new 关键字自己实例化。 Android操作系统应该这样做。以下是我要更改的内容:

    1. 不要让 SearchBar 扩展 Activity
    2. 为 SearchBar 提供一个public 方法(甚至可以是一个static 方法),该方法将Context 作为参数。使用它来获取NotificationManager 实例。

    像这样

    public class SearchBar {
        private SearchBar() {/* no instantiation */}
    
        public static void notifyPersistently(Context context) {
            NotificationManager nm = (NotificationManager)context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
            // all the rest ...
        }
    }
    

    【讨论】:

      【解决方案3】:

      同意@Karakuri aplly 他的建议和这段代码 试试下面的代码

      nm = (NotificationManager) context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
      

      【讨论】:

      • 现在在同一个地方得到NullPointerExceptionCaused by: java.lang.NullPointerException。我已经这样做了:Context context;context = SearchBar.this;nm = (NotificationManager) context .getSystemService(SearchBar.NOTIFICATION_SERVICE);
      • 这里可以使用nm = (NotificationManager) SearchBar.this.getSystemService(SearchBar.NOTIFICATION_SERVICE);
      • 再次出现同样的错误Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()getSystemService
      • 是的,因为 searchBar 不应该是一个活动。应用@Karakuri 答案。然后用我的线
      猜你喜欢
      • 2020-09-30
      • 2020-08-17
      • 2022-11-25
      • 2021-11-24
      • 2021-02-13
      • 2018-11-23
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多