【问题标题】:Displaying Snackbar from Utility class从 Utility 类显示 Snackbar
【发布时间】:2016-05-23 06:54:25
【问题描述】:

当我的实用程序类中引发异常时,我试图显示SnackBar

Utils.class

    public class Utils {

  private static String LOG_TAG = Utils.class.getSimpleName();

  public static boolean showPercent = true;

  public static ArrayList quoteJsonToContentVals(String JSON){
    ArrayList<ContentProviderOperation> batchOperations = new ArrayList<>();
    JSONObject jsonObject = null;
    JSONArray resultsArray = null;
    try{
      jsonObject = new JSONObject(JSON);
      if (jsonObject != null && jsonObject.length() != 0){
        jsonObject = jsonObject.getJSONObject("query");
        int count = Integer.parseInt(jsonObject.getString("count"));
        if (count == 1){
          jsonObject = jsonObject.getJSONObject("results")
              .getJSONObject("quote");
          batchOperations.add(buildBatchOperation(jsonObject));
        } else{
          resultsArray = jsonObject.getJSONObject("results").getJSONArray("quote");

          if (resultsArray != null && resultsArray.length() != 0){
            for (int i = 0; i < resultsArray.length(); i++){
              jsonObject = resultsArray.getJSONObject(i);
              batchOperations.add(buildBatchOperation(jsonObject));
            }
          }
        }
      }
    } catch (Exception e)
    {
      runError();  // <-- The method that displays the Snackbar
    }
    return batchOperations;
  }

而且,现在这里是尝试显示 SnackBar 的方法:

  private static void runError()
  {

    Snackbar.make(this.findViewById(android.R.id.content), "OOPS that's an error :/", Snackbar.LENGTH_LONG).show();
  }

现在,问题是:我无法让context 打电话给SnackBar。我尝试将this 替换为:

  • getContext()
  • getActivity()
  • getApplicationContext()

由于这些都不起作用,我尝试创建一个应用程序类来全局获取上下文。

    public class StockMain extends Application
{
    private static StockMain instance;

    public static StockMain getInstance() {
        return instance;
    }

    public static Context getContext(){
        return instance;
        // or return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }
}

并尝试将方法runError()更改为:

     private static void runError()
  {

    Snackbar.make(StockMain.getContext().findViewById(android.R.id.content), "OOPS that's an error :/", Snackbar.LENGTH_LONG).show();
  }

不幸的是,这也没有奏效! 那么,如何解决这个问题呢?

【问题讨论】:

  • 你不能在某处存储静态上下文或视图并使用它吗?
  • 您是否在任何活动中调用quoteJsonToContentVals 方法?
  • @ρяσѕρєяK 是的,我正在调用它,并且我得到了结果,当异常发生时,我也能够登录以登录 logcat,
  • @shaheen:当前代码中的问题是:StockMain.getContext() 行返回应用程序上下文和Snackbar.make 需要当前可见的 UI 屏幕上下文,因此如果您正在使用 Activity,请在 quoteJsonToContentVals ` 方法中传递 this致电findViewById(android.R.id.content)
  • 好的,谢谢@ρяσѕρєяK

标签: java android android-snackbar


【解决方案1】:

您可以在 Util 类中创建 构造函数,并将 Activity/Fragment 的上下文传递给 Util 类。像这样:

公共类实用程序{ 上下文 mContext;

// 创建构造函数

public Utils(Context context)
{
mContext = context;
}

// 其余代码 }

和你的 runError()

private void runError()
  {
  Snackbar.make(mContext.findViewById(android.R.id.content), "OOPS that's an error :/", Snackbar.LENGTH_LONG).show();
  }

【讨论】:

  • 这会导致内存泄漏,不要忘记在不需要的时候清空上下文
【解决方案2】:
  1. 尝试将view 作为参数传递给您的方法,如下所示-

    quoteJsonToContentVals(View view, String JSON)
    

其中view 可以是Activity 中引用的任何视图。

  1. 然后拨打runError(view);
  2. 改变方法如下-

    private static void runError(View view) {
        Snackbar.make(view, "OOPS that's an error :/", Snackbar.LENGTH_LONG).show();
    }
    

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 2020-05-16
    • 2016-04-24
    • 2019-08-28
    • 1970-01-01
    • 2018-12-02
    • 2019-12-15
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多