【问题标题】:System services not available to Activities before onCreate系统服务在 onCreate 之前对活动不可用
【发布时间】:2017-07-25 03:34:45
【问题描述】:

我的活动中有一个回收站视图。我在这个活动中实现了一个接口,当用户触摸回收器视图中的特定项目并将用户触摸的项目位置项目发送到活动时,它必须显示键盘。 这是我的界面:

public interface ReplyAction {
  void onEvent(Context context , int courseId);
}

这是我在活动中调用的接口方法:

 @Override
  public void onEvent(final Context mContext, int courseId) {

    replyTo = courseId;
    etMessage = (EditText) ((Activity) mContext).findViewById(R.id.etMessage);

    etMessage.setFocusable(true);

    //Show Keyboard to user
    InputMethodManager imm = (InputMethodManager) getSystemService(mContext.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  }

但我会收到此错误:

  FATAL EXCEPTION: main
                  Process: codenevisha.com.apps.learningmanagementsystem, PID: 7098
                  java.lang.IllegalStateException: System services not available to Activities before onCreate()
                      at android.app.Activity.getSystemService(Activity.java:5774)
                      at codenevisha.com.apps.learningmanagementsystem.activity.ActivityCourseChat.onEvent(ActivityCourseChat.java:547)
                      at codenevisha.com.apps.learningmanagementsystem.adapter.chatAdapter$1.onClick(chatAdapter.java:240)

这是我在回收器视图适配器中调用 OnEvent 方法的地方:

@Override
  public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    String question;
    String answer;

    switch (holder.getItemViewType()) {

      case 1: //For item message text left

        question = chatArray.get(position).getCourseForumModel().getCourseForumQuestion();
        answer = chatArray.get(position).getCourseForumModel().getCourseForumAnswer();

        ViewHolderLeftText vLText = (ViewHolderLeftText) holder;
        vLText.txtQuestion.setText(question);
        if (!answer.equals("")) {
          if (chatArray.get(position).getCourseForumModel().getCourseForumAUser().equals(G.userId)) {
            //This Answer is prepared by this user
            vLText.answerLayout.setBackgroundColor(mContext.getResources().getColor(R.color.my_message_background_color));
          }
          vLText.txtAnswer.setText(answer);

          //Handling reply message to this message
          vLText.imgReplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              ActivityCourseChat a = new ActivityCourseChat();
              a.onEvent(mContext , chatArray.get(position).getCourseForumModel().getCourseForumId());

            }
          });
        }
        break;

      case 2: //For item message image left
        ViewHolderLeftImage vLImage = (ViewHolderLeftImage) holder;

        //Handling reply message to this message
        vLImage.imgReply.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            ActivityCourseChat a = new ActivityCourseChat();
            a.onEvent(mContext ,chatArray.get(position).getCourseForumModel().getCourseForumId());

          }
        });
        break;
}

【问题讨论】:

  • 请编辑您的问题并发布整个 Java 堆栈跟踪。另外,什么叫onEvent(),那个代码在哪里?
  • 我在我的问题中添加了其余代码

标签: android interface


【解决方案1】:
ActivityCourseChat a = new ActivityCourseChat();

永远不要自己创建活动的实例。删除此行的两个匹配项。然后,以其他方式获取您的 ActivityCourseChat 实例。例如,如果ActivityCourseChat 正在使用这个RecyclerView.Adapter,则通过构造函数参数传入ActivityCourseChat 实例。

【讨论】:

  • 感谢您的简单而准确的回答。但是我可以请您解释一下为什么我自己创建一个活动的实例太糟糕了吗?
  • @Ehsan:因为它不起作用。 Activity 将无法正确初始化。
猜你喜欢
  • 2011-08-19
  • 2015-10-02
  • 1970-01-01
  • 2014-06-19
  • 2012-12-05
  • 2013-08-29
  • 1970-01-01
  • 2015-06-12
  • 2012-07-23
相关资源
最近更新 更多