android程序的创建过程,android 创建View过程分析

在调用findViewById()前,view对象其实已经创建好了,在setContextVIew的时候会创建layout中的控件.

setContentView(R.layout.main);

Activity中

public void setContentView(int layoutResID) {

getWindow().setContentView(layoutResID);

initActionBar();

}

window.class

/**

* Convenience for

* {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}

* to set the screen content from a layout resource.  The resource will be

* inflated, adding all top-level views to the screen.

*

* @param layoutResID Resource ID to be inflated.

* @see #setContentView(View, android.view.ViewGroup.LayoutParams)

*/

public abstract void setContentView(int layoutResID);

抽象方法的实现在哪里?

android.view.View.inflate()方法,追后会进入

/**

* Inflate a view from an XML resource.  This convenience method wraps the {@link

* LayoutInflater} class, which provides a full range of options for view inflation.

*

* @param context The Context object for your activity or application.

* @param resource The resource ID to inflate

* @param root A view group that will be the parent.  Used to properly inflate the

* layout_* parameters.

* @see LayoutInflater

*/

public static View inflate(Context context, int resource, ViewGroup root) {

LayoutInflater factory = LayoutInflater.from(context);

return factory.inflate(resource, root);

}

android.view.LayoutInflater.createView()方法:

/**

* Low-level function for instantiating a view by name. This attempts to

* instantiate a view class of the given name found in this

* LayoutInflater's ClassLoader.

*

*

* There are two things that can happen in an error case: either the

* exception describing the error will be thrown, or a null will be

* returned. You must deal with both possibilities -- the former will happen

* the first time createView() is called for a class of a particular name,

* the latter every time there-after for that class name.

*

* @param name The full name of the class to be instantiated.

* @param attrs The XML attributes supplied for this instance.

*

* @return View The newly instantiated view, or null.

*/

public final View createView(String name, String prefix, AttributeSet attrs)

throws ClassNotFoundException, InflateException {

Constructor extends View> constructor = sConstructorMap.get(name);

Class extends View> clazz = null;

try {

Trace.traceBegin(Trace.TRACE_TAG_VIEW, name);

if (constructor == null) {

// Class not found in the cache, see if it's real, and try to add it

clazz = mContext.getClassLoader().loadClass(

prefix != null ? (prefix + name) : name).asSubclass(View.class);

if (mFilter != null && clazz != null) {

boolean allowed = mFilter.onLoadClass(clazz);

if (!allowed) {

failNotAllowed(name, prefix, attrs);

}

}

constructor = clazz.getConstructor(mConstructorSignature);

sConstructorMap.put(name, constructor);

} else {

// If we have a filter, apply it to cached constructor

if (mFilter != null) {

// Have we seen this name before?

Boolean allowedState = mFilterMap.get(name);

if (allowedState == null) {

// New class -- remember whether it is allowed

clazz = mContext.getClassLoader().loadClass(

prefix != null ? (prefix + name) : name).asSubclass(View.class);

boolean allowed = clazz != null && mFilter.onLoadClass(clazz);

mFilterMap.put(name, allowed);

if (!allowed) {

failNotAllowed(name, prefix, attrs);

}

} else if (allowedState.equals(Boolean.FALSE)) {

failNotAllowed(name, prefix, attrs);

}

}

}

Object[] args = mConstructorArgs;

args[1] = attrs;

final View view = constructor.newInstance(args);

if (view instanceof ViewStub) {

// always use ourselves when inflating ViewStub later

final ViewStub viewStub = (ViewStub) view;

viewStub.setLayoutInflater(this);

}

return view;

} catch (NoSuchMethodException e) {

InflateException ie = new InflateException(attrs.getPositionDescription()

+ ": Error inflating class "

+ (prefix != null ? (prefix + name) : name));

ie.initCause(e);

throw ie;

} catch (ClassCastException e) {

// If loaded class is not a View subclass

InflateException ie = new InflateException(attrs.getPositionDescription()

+ ": Class is not a View "

+ (prefix != null ? (prefix + name) : name));

ie.initCause(e);

throw ie;

} catch (ClassNotFoundException e) {

// If loadClass fails, we should propagate the exception.

throw e;

} catch (Exception e) {

InflateException ie = new InflateException(attrs.getPositionDescription()

+ ": Error inflating class "

+ (clazz == null ? "" : clazz.getName()));

ie.initCause(e);

throw ie;

} finally {

Trace.traceEnd(Trace.TRACE_TAG_VIEW);

}

}

Constructor.class

/**

* Returns a new instance of the declaring class, initialized by dynamically

* invoking the constructor represented by this {@code Constructor} object.

* This reproduces the effect of {@code new declaringClass(arg1, arg2, ... ,

* argN)} This method performs the following:

*

*

A new instance of the declaring class is created. If the declaring

* class cannot be instantiated (i.e. abstract class, an interface, an array

* type, or a primitive type) then an InstantiationException is thrown.

*

If this Constructor object is enforcing access control (see

* {@link AccessibleObject}) and this constructor is not accessible from the

* current context, an IllegalAccessException is thrown.

*

If the number of arguments passed and the number of parameters do not

* match, an IllegalArgumentException is thrown.

*

For each argument passed:

*

*

If the corresponding parameter type is a primitive type, the argument

* is unboxed. If the unboxing fails, an IllegalArgumentException is

* thrown.

*

If the resulting argument cannot be converted to the parameter type

* via a widening conversion, an IllegalArgumentException is thrown.

*

*

The constructor represented by this {@code Constructor} object is

* then invoked. If an exception is thrown during the invocation, it is

* caught and wrapped in an InvocationTargetException. This exception is

* then thrown. If the invocation completes normally, the newly initialized

* object is returned.

*

*

* @param args

*            the arguments to the constructor

*

* @return the new, initialized, object

*

* @exception InstantiationException

*                if the class cannot be instantiated

* @exception IllegalAccessException

*                if this constructor is not accessible

* @exception IllegalArgumentException

*                if an incorrect number of arguments are passed, or an

*                argument could not be converted by a widening conversion

* @exception InvocationTargetException

*                if an exception was thrown by the invoked constructor

*

* @see AccessibleObject

*/

public T newInstance(Object... args) throws InstantiationException, IllegalAccessException,

IllegalArgumentException, InvocationTargetException {

return constructNative (args, declaringClass, parameterTypes, slot, flag);

}

private nativeT constructNative(Object[] args, Class declaringClass,

Class>[] parameterTypes, int slot,

boolean noAccessCheck) throws InstantiationException, IllegalAccessException,

InvocationTargetException;

//C++中可调用Java代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值