【问题标题】:Yellow highlight while using custom Toast in android在android中使用自定义Toast时出现黄色突出显示
【发布时间】:2016-04-19 08:56:17
【问题描述】:

我正在尝试实现自定义 toast,下面是我编写的代码并将其挂接到 onClickeListner 上

Button customToastButton = (Button) this.findViewById(R.id.add_to_cart);
        customToastButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //get the LayoutInflater and inflate the custom_toast layout
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.cart_toast, (ViewGroup)
                        findViewById(R.id.toast_layout_root));

                //get the TextView from the custom_toast layout
                TextView text = (TextView) layout.findViewById(R.id.toastText);
                text.setText("Item as been added to cart");

                //create the toast object, set display duration,
                //set the view as layout that's inflated above and then call show()
                Toast t = new Toast(getApplicationContext());
                t.setDuration(Toast.LENGTH_SHORT);
                t.setView(layout);
                t.show();
            }
        }); 

使用的代码以黄色突出显示,显示以下错误。它没有在 logcat 中显示为错误,代码工作正常。highlightend 部分还说“可能产生'java.lang.NullPointerException' em>

Method invocation 'customToastButton.setOnClickListener(new View.OnClickListener() {             
public void onClick(Vi...' may produce 'java.lang.NullPointerException' less... (Ctrl+F1) 
    This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.
    Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors.
    More complex contracts can be defined using @Contract annotation, for example:
    @Contract("_, null -> null") — method returns null if its second argument is null 
@Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise 
@Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it 
    The inspection can be configured to use custom @Nullable
    @NotNull annotations (by default the ones from annotations.jar will be used)

我只是不明白导致此错误的原因,请您查看一下。 提前谢谢你

【问题讨论】:

  • findViewById 可能返回 null ...所以customToastButton.setOnClickListener 可能会产生 NPE ...这只是一个警告 ...您可以使用注释/注释或进行空检查来抑制警告跨度>
  • NPE.?无论如何要删除警告?

标签: android android-studio toast android-toast


【解决方案1】:

你可以在设置OnClickListener之前检查你的customToastButton是否不为空

if(customToastButton!=null){
    customToastButton.setOnClickListener(...)
...
}

显示警告,因为如果 Button 不存在而您继续使用它,则会抛出 NullPointerException

【讨论】:

  • 像魅力一样工作 感谢您的支持,这对我来说意义重大
  • 确定没问题!如果您能接受正确的答案,那就太好了:)
  • 当然,顺便说一句,知道任何与在 android 中开发购物车相关的事情。?就像传递数据的提示就足够了
  • 抱歉,没有这方面的经验。但肯定有一些不错的教程,你可以看看
  • 再次感谢您。
猜你喜欢
  • 2014-06-22
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多