【问题标题】:How resolve multiple context in android:name tag? I'm using volley, json如何解析 android:name 标签中的多个上下文?我正在使用凌空抽射,json
【发布时间】:2016-02-10 09:11:23
【问题描述】:

按照这篇文章参考

getApplicationContext()' on a null object reference using volley in non-activity

这是我的舞台:

登录 >(呼叫客户)Cliente.class> 转到“Welcome.class

运动 >(调用运动帐户)Movimientos.class>返回运动

帐户 >(调用列表帐户)Cuentas.class>返回帐户

但我只能添加到 AndroidManifest android:name="restful.dao.Cliente"

<application
    android:name="restful.dao.Cliente"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

所有这些都使用 URL volley 和 JSON。

他们,如何为所有非活动添加属性上下文? - Cliente.class - Movimientos.class - Cuentas.class

考虑到我不能添加多个标签

  • android:name="restful.dao.Cliente"
  • android:name="restful.dao.Movimientos"
  • android:name="restful.dao.Cuentas"

【问题讨论】:

    标签: android json rest android-volley


    【解决方案1】:

    您可以将Context 作为参数传递给非活动类,并引用它而不是调用getApplicationContext()

    public class Cliente {
        private Context mContext;
    
        public Cliente(Context context) {
            mContext = context;
    
            // Now you can reference mContext 
            // instead of calling getApplicationContext()
        }
    }
    

    现在使用 new Cliente(this);new Cliente(getApplicationContext()); 实例化 Cliente 类,具体取决于您在代码中实例化 Cliente 类的位置。

    【讨论】:

    • 这只返回空上下文
    【解决方案2】:

    您只能拥有一个扩展应用程序的类。让它类似于 MyApp。

    然后,无论您需要上下文,都可以从调用 Activity 或 MyApp 实例中获取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 2014-02-22
      • 2015-10-30
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多