这是我正在处理的问题。
我正在尝试设置ViewPagers
以加载3 XML
个文件。
我们称之为ViewPagerAdapter.java
在对象Instantiate上,我尝试使用findViewWithTag()
更改所有文本字段字体。
但是,如果使用EditText
定义了findViewWithTag()
,则会返回导致null
错误的system.out.println
(使用setTypeFace
)。
我的代码做错了吗?
Object Instantiate Code (ViewPagerAdapter.java)
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(position) {
case 0:
resId = R.layout.activity_login;
break;
case 1:
resId = R.layout.activity_register;
break;
case 2:
resId = R.layout.activity_vote;
break;
}
View view = inflater.inflate(resId, null);
EditText txtFields = (EditText) view.findViewWithTag("abeezee");
txtFields.setTypeFace(Typeface.DEFAULT_BOLD); //This one cause the error (Line 39)
System.out.println(txtFields);
((ViewPager) collection).addView(view, 0);
return view;
}
其中一个XML(activity_register.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layRegister"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/tvRegisterTitle"
style="@style/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etNameReg"
android:layout_alignParentTop="true"
android:text="@string/register_text"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="@+id/etNameReg"
style="@style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spVoteMlm"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/name_hint"
android:inputType="textPersonName"
android:tag="abeezee"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/etEmailReg"
style="@style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etNameReg"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/email_hint"
android:inputType="textEmailAddress"
android:tag="abeezee"
/>
<EditText
android:id="@+id/etPasswordReg"
style="@style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etEmailReg"
android:layout_below="@+id/etEmailReg"
android:layout_centerVertical="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/password_hint"
android:typeface="serif"
android:inputType="textPassword"
android:tag="abeezee"
/>
<Spinner
android:id="@+id/spVoteMlm"
style="@style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/tvRegisterTitle"
android:layout_marginTop="30dp"
android:entries="@array/mlm_list"
android:tag="abeezee"
/>
<Button
android:id="@+id/btnRegister"
style="@style/buttonBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnTerms"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/register_text" />
<Button
android:id="@+id/btnTerms"
style="@style/smallLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etPasswordReg"
android:layout_centerHorizontal="true"
android:gravity="center"
android:onClick="showTermsPolicy"
android:text="@string/terms_policy" />
</RelativeLayout>
Logcat错误:
09-24 13:17:49.178: E/AndroidRuntime(16278): FATAL EXCEPTION: main
09-24 13:17:49.178: E/AndroidRuntime(16278): java.lang.NullPointerException
09-24 13:17:49.178: E/AndroidRuntime(16278): at com.sociyo.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:39)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.View.measure(View.java:15392)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:622)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:404)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.View.measure(View.java:15392)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.View.measure(View.java:15392)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.View.measure(View.java:15392)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
09-24 13:17:49.178: E/AndroidRuntime(16278): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.View.measure(View.java:15392)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1865)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1286)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1011)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4244)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.os.Handler.handleCallback(Handler.java:615)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.os.Handler.dispatchMessage(Handler.java:92)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.os.Looper.loop(Looper.java:213)
09-24 13:17:49.178: E/AndroidRuntime(16278): at android.app.ActivityThread.main(ActivityThread.java:4793)
09-24 13:17:49.178: E/AndroidRuntime(16278): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 13:17:49.178: E/AndroidRuntime(16278): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 13:17:49.178: E/AndroidRuntime(16278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-24 13:17:49.178: E/AndroidRuntime(16278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-24 13:17:49.178: E/AndroidRuntime(16278): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您可以使用以下getViewsByTag
函数从父元素进行迭代,并获取它包含的所有“已标记”子元素。然后,您可以迭代这些视图并使用switch
语句和instanceof
来处理Button
,EditText
或TextView
元素,如果您愿意的话。
public static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
}