下面列出了怎么用android.view.InflateException的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressLint("InflateParams")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View customView;
try {
customView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_help_view, null);
} catch (InflateException e) {
throw new IllegalStateException("This device does not support Web Views.");
}
MaterialDialog dialog =
new MaterialDialog.Builder(getActivity())
.theme(getArguments().getBoolean("dark_theme") ? Theme.DARK : Theme.LIGHT)
.title("帮助信息")
.customView(customView, false)
.positiveText(android.R.string.ok)
.build();
final WebView webView = customView.findViewById(R.id.webview);
// webView.loadData(this.content, "text/html", "UTF-8");
WebViewUtil.LoadHtmlIntoWebView(webView,this.content,getActivity(),"");
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Defines the xml file for the fragment
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_camera, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
private static View createView(Context context, String name, String prefix) throws InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
Log.w("BackgroundLibrary", "cannot create 【" + name + "】 : ");
return null;
}
}
private static View createView(Context context, String name, String prefix) throws InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
Log.w("BackgroundLibrary", "cannot create 【" + name + "】 : ");
return null;
}
}
@SuppressLint("InflateParams")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View customView;
try {
customView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_help_view, null);
} catch (InflateException e) {
throw new IllegalStateException("This device does not support Web Views.");
}
MaterialDialog dialog =
new MaterialDialog.Builder(getActivity())
.theme(getArguments().getBoolean("dark_theme") ? Theme.DARK : Theme.LIGHT)
.title("帮助信息")
.customView(customView, false)
.positiveText(android.R.string.ok)
.build();
final WebView webView = customView.findViewById(R.id.webview);
// webView.loadData(this.content, "text/html", "UTF-8");
WebViewUtil.LoadHtmlIntoWebView(webView,this.content,getActivity(),"");
return dialog;
}
private static View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
protected void initInflate(Context context) {
try {
View.inflate(context, getLayoutId(), this);
} catch (InflateException e) {
if (e.toString().contains("GSYImageCover")) {
Debuger.printfError("********************\n" +
"***** 注意 *****" +
"********************\n" +
"*该版本需要清除布局文件中的GSYImageCover\n" +
"**** Attention ***\n" +
"*Please remove GSYImageCover from Layout in this Version\n" +
"********************\n");
e.printStackTrace();
throw new InflateException("该版本需要清除布局文件中的GSYImageCover,please remove GSYImageCover from your layout");
} else {
e.printStackTrace();
}
}
}
public void inflateMenu(@MenuRes int menuRes) {
if(menuRes == 0) return;
if (isInEditMode()) return;
getActivity().getMenuInflater().inflate(menuRes, mActionMenu.getMenu());
XmlResourceParser parser = null;
try {
//noinspection ResourceType
parser = getResources().getLayout(menuRes);
AttributeSet attrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs);
} catch (XmlPullParserException | IOException e) {
// should not happens
throw new InflateException("Error parsing menu XML", e);
} finally {
if (parser != null) parser.close();
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.establishment_fragment, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
ButterKnife.bind(this, view);
((MainActivity) getActivity()).setToolbarTitle(getContext().getString(R.string.establishments_title));
toggleFabButton(false);
mPresenter = new EstablishmentPresenterImpl(this, getContext(), getActivity());
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(mPresenter);
return view;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
int layoutResID = initView(savedInstanceState);
//如果initView返回0,框架则不会调用setContentView(),当然也不会 Bind ButterKnife
if (layoutResID != 0) {
setContentView(layoutResID);
//绑定到butterknife
mUnbinder = ButterKnife.bind(this);
}
} catch (Exception e) {
if (e instanceof InflateException) {
throw e;
}
e.printStackTrace();
}
initData(savedInstanceState);
}
@Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(getLayout(), container, false);
} catch (InflateException e) {
e.printStackTrace();
}
return rootView;
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
if (root != null)
{
ViewGroup parent = (ViewGroup) root.getParent();
if (parent != null)
{
parent.removeView(root);
}
}
try
{
root = inflater.inflate(R.layout.tu_baike_main, container, false);
initView(root);
bindEvents();
}
catch (InflateException e)
{
/* map is already there, just return view as it is */
}
return root;
}
/**
* Prepares an alert dialog builder, using the work_dialog view.
* <p>
* The caller should finish populating the builder, then call AlertDialog.Builder#show().
*/
public static AlertDialog.Builder create(Activity activity, int titleId) {
View view;
try {
view = activity.getLayoutInflater().inflate(R.layout.work_dialog, null);
} catch (InflateException ie) {
Log.e(TAG, "Exception while inflating work dialog layout: " + ie.getMessage());
throw ie;
}
String title = activity.getString(titleId);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(title);
builder.setView(view);
return builder;
}
/**
* Displays the About box. An AlertDialog is created in the calling activity's context.
* <p>
* The box will disappear if the "OK" button is touched, if an area outside the box is
* touched, if the screen is rotated ... doing just about anything makes it disappear.
*/
public static void display(Activity caller) {
String versionStr = getVersionString(caller);
String aboutHeader = caller.getString(R.string.app_name) + " v" + versionStr;
// Manually inflate the view that will form the body of the dialog.
View aboutView;
try {
aboutView = caller.getLayoutInflater().inflate(R.layout.about_dialog, null);
} catch (InflateException ie) {
Log.e(TAG, "Exception while inflating about box: " + ie.getMessage());
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(caller);
builder.setTitle(aboutHeader);
builder.setIcon(R.drawable.ic_launcher);
builder.setCancelable(true); // implies setCanceledOnTouchOutside
builder.setPositiveButton(R.string.ok, null);
builder.setView(aboutView);
builder.show();
}
public static boolean fixException(Object obj, Throwable e) {
Class exceptionClass = e.getClass();
Log.e(TAG, obj.getClass().getName() + " throws " + e.getClass().getName() + " exception.");
if (exceptionClass.equals(IllegalStateException.class)) {
// You need to use a Theme.AppCompat theme (or descendant) with this activity.
if (e.getMessage().startsWith("You need to use a Theme.AppCompat")) {
dumpAssets(obj, true);
}
} else if (exceptionClass.equals(InflateException.class)) {
dumpAssets(obj, false);
} else if (exceptionClass.equals(Resources.NotFoundException.class)) {
dumpAssets(obj, false);
}
return false;
}
public void inflateMenu(@MenuRes int menuRes) {
if(menuRes == 0) return;
if (isInEditMode()) return;
getActivity().getMenuInflater().inflate(menuRes, mActionMenu.getMenu());
XmlResourceParser parser = null;
try {
//noinspection ResourceType
parser = getResources().getLayout(menuRes);
AttributeSet attrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs);
} catch (XmlPullParserException | IOException e) {
// should not happens
throw new InflateException("Error parsing menu XML", e);
} finally {
if (parser != null) parser.close();
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException
{
Constructor<? extends View> constructor = sConstructorMap.get(name);
try
{
if (constructor == null)
{
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e)
{
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
* In some cases like system updating the WebView, it is not available for rendering and the
* system raises an InflateException. To handle this case we're showing a full-screen error
* with reload button.
* More info on JIRA story: LEARNER-7267
* */
try {
isSystemUpdatingWebView = false;
return inflater.inflate(R.layout.fragment_authenticated_webview, container, false);
} catch (InflateException e) {
logger.error(e, true);
isSystemUpdatingWebView = true;
return inflater.inflate(R.layout.content_error, container, false);
}
}
private View createView(String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = getClassLoader(mContext, name, prefix).loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
private void init(AttributeSet attrs) {
maskResourceId = -1;
TypedArray array = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.MaskedImageView,
ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE);
try {
maskResourceId = array.getResourceId(R.styleable.MaskedImageView_mask, -1);
} finally {
array.recycle();
}
if (maskResourceId < ConstsCore.ZERO_INT_VALUE) {
throw new InflateException("Mandatory 'mask' attribute not set!");
}
maskedPaint = new Paint();
maskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
copyPaint = new Paint();
maskDrawable = getResources().getDrawable(maskResourceId);
}
public void startWindowManager() {
Logd(TAG, "Starting up the window manager...");
if (v != null) {
((WindowManager) cs.getSystemService("window")).removeView(v);
v = null;
}
WindowManager wm = (WindowManager) cs.getSystemService("window");
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(1, 1, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT); //width, height, _type (2007), _flags (32), _format (-3)
lp.y = 0;
lp.x = 0;
lp.gravity = Gravity.LEFT;
try {
v = ((LayoutInflater) cs.getSystemService("layout_inflater")).inflate(R.layout.camera, null);
} catch (InflateException e) {
Logd(TAG, "Error: " + e.getLocalizedMessage());
}
v.setVisibility(0);
wm.addView(v, lp);
sv = ((SurfaceView) v.findViewById(R.id.surfaceView));
sh = sv.getHolder();
sh.addCallback(this);
}