【问题标题】:Background worker for android studio "this" parameter errorandroid studio“this”参数错误的后台工作者
【发布时间】:2017-07-31 23:28:48
【问题描述】:

我使用此代码初始化我的后台工作人员。 BackgroundWorker 是一个类。 出于某种原因,“this”表示“无法应用”。

BackgroundWorker backgroundWorker = new BackgroundWorker(this);

===该区域的更多代码===

    mApp.setListener(new OAuthAuthenticationListener() {

        @Override
        public void onSuccess() {

            // tvSummary.setText("Connected as " + mApp.getUserName());
            btnConnect.setText("Disconnect");
            llAfterLoginView.setVisibility(View.VISIBLE);
            // userInfoHashmap = mApp.
            mApp.fetchUserName(handler);
        }
        public void OnLogin() {

            String username = "hi";
            String password = "hi";
            String type = "register";
            BackgroundWorker backgroundWorker = new BackgroundWorker(this); // <-- that one
            backgroundWorker.execute(type, username, password);
        }

        @Override
        public void onFail(String error) {
            Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT)
                    .show();
        }
    });

===工人===

https://pastebin.com/rMZqvvhF

上面的代码是持有BackgroundWorker的类。在这堂课中。它连接并添加或登录到服务器。我是新手,希望能得到帮助。

=========编辑(附加帮助)=========

感谢大家的帮助,但我有一个问题。当我将其更改为 getApplicationData 时,它会运行后台工作程序,但随后会使应用程序崩溃。有什么想法吗?

=========编辑=========

看起来您的 Backgroundworker 为构造函数接收了一个 Context。不要使用“this”,而是尝试使用 getApplicationContext() 来获取上下文。

【问题讨论】:

  • BackgroundWorker 是标准库类吗?我没有看到它的在线文档
  • 我自己做的java类
  • 如果我完全错了。请指导我如何完成这项任务
  • 如果BackgroundWorker是你写的一个类,如果你不提供它的源代码,谁能告诉你为什么它不起作用?
  • 好点一秒

标签: android server backgroundworker


【解决方案1】:

因为您的示例中的this 引用OAuthAuthenticationListener 而不是Activity。所以使用类似的东西:

BackgroundWorker backgroundWorker = new BackgroundWorker(getBaseContext());

如果您的代码在 Fragment 内,请改用 getContext()

【讨论】:

    【解决方案2】:

    看起来您的 Backgroundworker 为构造函数接收了一个 Context。不要使用“this”,而是尝试使用 getApplicationContext() 来获取上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多