Android Toast资源未找到资源$NotFoundException:文件res/layout/transient\u notification.xml 这个问题并不总是存在,我找不到问题的原因,寻求帮助,谢谢。

Android Toast资源未找到资源$NotFoundException:文件res/layout/transient\u notification.xml 这个问题并不总是存在,我找不到问题的原因,寻求帮助,谢谢。,android,resources,toast,Android,Resources,Toast,我的代码是 private static Toast systemToast; public static Toast getSystemToast(Object resId) { if (null == systemToast) { // Apps is the Application.java systemToast = Toast.makeText(Apps.getAppContext(), R.string.me_empty,

我的代码是

private static Toast systemToast;       

public static Toast getSystemToast(Object resId) {
    if (null == systemToast) {
         // Apps is the Application.java
        systemToast = Toast.makeText(Apps.getAppContext(), R.string.me_empty,
                Toast.LENGTH_SHORT);
    }

    String res = String.valueOf(resId);
    if (resId.getClass() == Integer.class) {
        systemToast.setText(Integer.valueOf(res));
    } else if (resId.getClass() == String.class) {
        systemToast.setText(res);
    }
    systemToast.setDuration(Toast.LENGTH_SHORT);
    return systemToast;
}
/**Apps.java**/

  public class Apps extends Application {

    private static Apps sContext;
    @Override
protected void attachBaseContext(Context base) {
    sContext = this;
}
public static Apps getAppContext() {
    return sContext;
}

在某些Android设备中,发生错误,错误日志为:

  android.content.res.Resources$NotFoundException: File res/layout
/transient_notification.xml from xml type layout resource ID #0x10900ef at 
android.content.res.Resources.loadXmlResourceParser(Resources.java:2720) at 
android.content.res.Resources.loadXmlResourceParser(Resources.java:2675) at 
android.content.res.Resources.getLayout(Resources.java:1096) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:422) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:368) at     
android.widget.Toast.makeText(Toast.java:282) 

当传递上下文对象未正确初始化或其引用为null时,可能会发生此错误

1.如果您使用的是Fragment than,那么可以在onAttach方法中找到上下文。并传递您的getSystemToast方法

 **@Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }**
二,。如果您使用的是Activity,则使用getBaseContext()方法或ActivityName获取上下文。这两种方法都将返回您的上下文

您不需要为get Context定义函数。Android为获取上下文提供以下方法。 1.getApplicationContext()应用程序上下文与应用程序关联,并且在整个生命周期中始终相同

2.getBaseContext()

3.片段中的onAttach()