GVKun编程网logo

java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“

2

本文将为您提供关于java–“警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了InstantRun)“的详细介绍,同时,我们还将为您提供关于.NetMAUI“run-as”命令

本文将为您提供关于java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“的详细介绍,同时,我们还将为您提供关于.Net MAUI“run-as”命令失败,“run-as:无法设置功能:不允许操作”、Async / Await, Task.Run(async() => await), await Task.Run(()=>), await Task.Run(async() => await) 区别、cnpm run dev或npm run dev问题收集、docker-compose run mysql: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)的实用信息。

本文目录一览:

java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“

java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“

类似的问题一直是asked here,here和here,但背景与此截然不同,而且code that gave from this error是由Android和Android Studio的制造商编写的.

这是代码:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap>
                    cache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or broadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getimageLoader() {
        return mImageLoader;
    }
}

发出警告的行是:

private static MySingleton mInstance;
private static Context mCtx;

现在,如果我删除static关键字,请更改public static synchronized MySingleton getInstance(Context … to public synchronized MySingleton getInstance(Context …错误disappers但是出现了另一个问题.

我在RecyclerView中使用MySingleton.所以这一行

@覆盖
    public void onBindViewHolder(final RecyclerView.ViewHolder holder,int position){
        ImageLoader imageLoader = MySingleton.getInstance(mContext).getimageLoader();

告诉我

Non-static method ‘getInstance(android.content.Context)’ cannot be refrenced from a static context.

请有人知道如何解决这个问题?

解决方法:

我在answer to a similar question answered by CommonsWare找到了解决方案

我引用

The quoted Lint warning is not complaining about creating singletons.
It is complaining about creating singletons holding a reference to an
arbitrary Context, as that Could be something like an Activity.
Hopefully, by changing mContext = context to mContext =
context.getApplicationContext(), you will get rid of that warning
(though it is possible that this still breaks Instant Run — I cannot
really comment on that).

Creating singletons is fine, so long as you do so very carefully, to
avoid memory leaks (e.g., holding an indefinite static reference to an
Activity).

因此谷歌实际上并没有签约.要解决此问题,如果this.getApplicationContext作为上下文的参数提供,则不会发生内存泄漏.

所以实质上,忽略警告并提供this.getApplicationContext作为上下文的参数.

.Net MAUI“run-as”命令失败,“run-as:无法设置功能:不允许操作”

.Net MAUI“run-as”命令失败,“run-as:无法设置功能:不允许操作”

如何解决.Net MAUI“run-as”命令失败,“run-as:无法设置功能:不允许操作”?

我最近使用 VS2022 Version 17.0.0 Preview 2.1 更新到 MAUI Preview 6,我已经在运行的 MAUI 项目开始抛出以下错误:

严重性代码描述项目文件行抑制状态 错误 XA0137:''run-as 命令失败,''run-as:无法设置功能:不允许操作 ''。 此设备当前不支持快速部署。 请使用 Visual Studio 中的“帮助-> 发送反馈-> 报告问题”菜单项提交带有确切错误消息的问题 或 Visual Studio for Mac 中的“帮助-> 报告问题”。 请将“EmbedAssembliesIntoApk”MSBuild 属性设置为“true”以在 Visual Studio 项目属性页中禁用快速部署,或在文本编辑器中编辑项目文件。

有什么我遗漏的吗?如果有人有可能的解决方案,请告诉我。

此外,即使我创建一个新项目,我也能看到这个问题。

另外,我已经执行了 Maui 检查,我的安装似乎是正确的。

解决方法

睡个好觉后我显然意识到,我的答案就在我的问题中!

添加

 <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>

我的 MAUI 项目属性组解决了这个问题,我可以再次运行。

我猜这个问题是由于快速渲染器无法与我当前在 Android 7 上运行的调试设备一起工作造成的

Async / Await, Task.Run(async() => await), await Task.Run(()=>), await Task.Run(async() => await) 区别

Async / Await, Task.Run(async() => await), await Task.Run(()=>), await Task.Run(async() => await) 区别

如何解决Async / Await, Task.Run(async() => await), await Task.Run(()=>), await Task.Run(async() => await) 区别

嗨,我是 Xamarin 开发人员

我有一些关于使用异步编程的问题

当我调用 Webservice 时,我通常使用 async/await。

但是,如果它只使用一个线程(UI),那么在 UI 更改时会导致一些问题。

例如,在获取价值之前先处理代码,有时会挂起UI

我阅读了一些关于异步编程的文档或博客。这表明您可以解决使用 Task.RUN 时的问题

但是很难知道我的编程水平.....

这是一些例子,你能解释一下有什么不同吗?

notificationVos = await Task.Run(() => wSSmartConsoleCommon.getScmessage<NotificationVo>());

notificationVos = Task.Run(() => wSSmartConsoleCommon.getScmessage<NotificationVo>());

notificationVos = await wSSmartConsoleCommon.getScmessage<NotificationVo>());

notificationVos = await Task.Run(async() => await wSSmartConsoleCommon.getScmessage<NotificationVo>());

cnpm run dev或npm run dev问题收集

cnpm run dev或npm run dev问题收集

#vue项目中遇到的问题

跟着 拯救地球好累丫的简书文章上的操作,遇到的问题

vue入门 | 使用vue.js2.0 + ElementUI开发后台管理系统详细教程(一)

vue入门 | 使用vue.js2.0 + ElementUI开发后台管理系统详细教程(二)

##1.解除端口占用方法

  • 1.1首先点击开始菜单选择运行,接着在运行对话框中输入“cmd”,回车打开命令提示符窗口,然后在窗口中输入【netstat -ano】,按下回车,之后就会显示所有的端口占用情况。
C:\Users\Administrator>netstat -ano
活动连接
  协议  本地地址          外部地址        状态           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       880
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:902            0.0.0.0:0              LISTENING       4940
  TCP    0.0.0.0:912            0.0.0.0:0              LISTENING       4940
  TCP    0.0.0.0:1025           0.0.0.0:0              LISTENING       564
  TCP    0.0.0.0:1026           0.0.0.0:0              LISTENING       968
  TCP    0.0.0.0:1027           0.0.0.0:0              LISTENING       412
  TCP    0.0.0.0:1028           0.0.0.0:0              LISTENING       668
  TCP    0.0.0.0:1065           0.0.0.0:0              LISTENING       632
 TCP    [::1]:8080             [::1]:8080             ESTABLISHED     792
  • 2.1如果你要查询指定的端口占用的话,可以在窗口中继续输入【netstat -aon|findstr "提示的端口"】,这里的提示的端口假设为8080,那么就输入命令为【netstat -aon|findstr "8080"】,
C:\Users\Administrator>netstat -ano|findstr "8080"
  TCP    [::1]:8080             [::1]:8080             ESTABLISHED     792

注意findstr后面有空格;

  • 3.1然后根据查询的PID找到对应的进程,我么可以看到占有80这个程序的进程ID:792,继续输入命令【tasklist|findstr "792"】,792就是进程ID,现在知道是哪个进程占用的我们就可以采取相应措施进行解决了。
C:\Users\Administrator>tasklist|findstr "792"
QQ.exe                        5856 Console                    1    128,792 K
360se.exe                      792 Console                    1    132,752 K

##2.cnpm run dev 报错

  • 2.1These dependencies were not found in node_modules
 ERROR  Failed to compile with 2 errors
These dependencies were not found in node_modules:
*ansi-html in ./~/.2.17.0@webpack-hot-middleware/client-overlay.js
*html-entities in ./~/.2.17.0@webpack-hot-middleware/client-overlay.js

因为package.json中"webpack-hot-middleware": "^2.16.1",和运行的版本不一致导致,修改为“^2.17.0”,也不管用,运行

cnpm uninstall webpack-hot-middleware --save-dev

再运行

cnpm install webpack-hot-middleware@2.16.1 --save-dev
  • 2.2error in ./src/main.js 检查main.js空行
 error  in ./src/main.js
  ✘  http://eslint.org/docs/rules/no-multiple-empty-lines  More than 1 blank line not allowed
  D:\download\backup\workspace\MyApp\Vuejs\my-project\src\main.js:6:1
   ^
✘ 1 problem (1 error, 0 warnings)
Errors:
 1  http://eslint.org/docs/rules/no-multiple-empty-lines
 @ multi ./build/dev-client ./src/main.js

src\main.js代码中:

<script>
  import Vue from ''vue''
  import Element from ''element-ui''
  import ''element-ui/lib/theme-default/index.css''
  Vue.use(Element)
  export default {
    name: ''app'',
    data: function () {
      return {
        active: true
      }
    }
  }
</script>
  • 2.3error in ./src/App.vue App.vue中<script>中必须含 import Vue from ''vue''
 error  in ./src/App.vue
✘  http://eslint.org/docs/rules/no-undef  ''Vue'' is not defined
  D:\download\backup\workspace\MyApp\Vuejs\my-project\src\App.vue:37:3
  Vue.use(Element)
     ^
✘ 1 problem (1 error, 0 warnings)
Errors:
  1  http://eslint.org/docs/rules/no-undef
 @ ./src/main.js 3:0-24
 @ multi ./build/dev-client ./src/main.js

src\main.js代码中:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ''vue''
import App from ''./App''
import router from ''./router''
/* eslint-disable no-new */
new Vue({
  el: ''#app'',
  router,
  template: ''<App/>'',
  components: { App }
})
  • 2.4关闭eslint检查,把不需要检查的文件名加到/.eslintignore文件中

docker-compose run mysql: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

docker-compose run mysql: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

如何解决docker-compose run mysql: ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/run/mysqld/mysqld.sock'' (2)

连续工作 14 小时后我完全被烤焦了,即使在整个互联网上爬行后我也无法解决这个问题。

场景很简单:

我有一个 docker-compose.yml,其中包含安装和部署应用程序所需的许多服务,其中包括一个简单的 MysqL 5 服务:

  (...)
  MysqL:
    environment:
      MysqL_ALLOW_EMPTY_PASSWORD: "yes"
      MysqL_ROOT_PASSWORD: 
    image: MysqL:5
    restart: always
    volumes:
      - .MysqL:/var/lib/MysqL
  (...)

现在我只想在部署应用程序之前创建初始数据库,使用一个简单的:

docker-compose run MysqL MysqL -e "CREATE DATABASE IF NOT EXISTS database"

但是无论我怎么尝试,都会出现这个错误:

Creating network "app_default" with the default driver
Creating app_MysqL_run ... done
ERROR 2002 (HY000): Can''t connect to local MysqL server through socket ''/var/run/MysqLd/MysqLd.sock'' (2)

我也试过(没有成功/有同样的错误信息):

docker-compose up -d MysqL
docker-compose exec MysqL MysqL -e "CREATE DATABASE IF NOT EXISTS database"

唯一有效的是进入容器并手动执行exact(!) same(!)命令。但我绝对需要自动化。

我错过了什么?任何形式的帮助都非常感谢!

解决方法

结果 docker-compose rundocker-compose exec 覆盖了 Dockerfile 的命令,因此 mysqld 永远不会启动。传递 mysqld && mysql -e "CREATE DATABASE database" 不起作用,在 docker-compose exec 之后运行 docker-compose up 将导致竞争条件。

我最终得到了适合我需求的以下解决方案:

docker-compose run mysql && \\
docker-compose exec mysql /bin/bash -c "while ! mysql -e \\"CREATE DATABASE IF NOT EXISTS database\\" &> /dev/null; do sleep 1; done"

这将在子容器上循环命令,直到主机容器准备就绪。此外,IF NOT EXISTS 在这里很重要,否则如果数据库已经存在,可能会陷入死锁。

关于java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于.Net MAUI“run-as”命令失败,“run-as:无法设置功能:不允许操作”、Async / Await, Task.Run(async() => await), await Task.Run(()=>), await Task.Run(async() => await) 区别、cnpm run dev或npm run dev问题收集、docker-compose run mysql: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)等相关内容,可以在本站寻找。

本文标签: