在onCreate之前,活动无法使用系统服务

时间:2017-07-24 19:12:58

标签: android interface

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

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

这是我在acivity中调用的界面方法:

 @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)

这是我在我的Recycler视图适配器中调用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;
}

1 个答案:

答案 0 :(得分:1)

ActivityCourseChat a = new ActivityCourseChat();

从不自己创建活动的实例。删除此行的两次出现。然后,以其他方式获取您的ActivityCourseChat实例。例如,如果RecyclerView.Adapter正在使用此ActivityCourseChat,请通过构造函数参数传入ActivityCourseChat实例。