我在运行时遇到空指针异常。我正在从 PagerView 的 2 个 XML 文件中提取相对布局,我似乎搞砸了。知道是什么导致了错误吗?
谢谢你的帮助!
我有两个具有相同结构的 XML 文件——player_turn_solution_component.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/player_turn_solution_component"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/solutionCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="what do you see?" />
<EditText
android:id="@+id/solutionInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/solutionCaption"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
我的实例化功能:
@Override
public Object instantiateItem(View collection, int position) {
//Our specific component (TextView or ImageView, depending on posision
//is placed inside a RelativeLayout that acts as a container
RelativeLayout container = new RelativeLayout(pagerContext);
TextView caption = new TextView(pagerContext);
caption.setText("View: " + position);
caption.setTypeface(typeface);
caption.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
container.addView(caption);
RelativeLayout optionLayout = null;
TextView optionCaption = null;
if (position == 0) {
optionLayout = (RelativeLayout) findViewById(R.id.player_turn_solution_component);
optionCaption = (TextView) findViewById(R.id.solutionCaption);
} else if (position == 1) {
optionLayout = (RelativeLayout) findViewById(R.id.player_turn_clue_component);
optionCaption = (TextView) findViewById(R.id.clueCaption);
}
container.addView(optionLayout);
((ViewPager) collection).addView(container,0); //get the container ready
return container;
}
和 LogCat 输出:
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewGroup.addView(ViewGroup.java:3158)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewGroup.addView(ViewGroup.java:3145)
07-06 09:30:22.337: E/AndroidRuntime(8418): at my.app.PlayerTurnActivity$HelpPagerAdapter.instantiateItem(PlayerTurnActivity.java:136)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1375)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-06 09:30:22.337: E/AndroidRuntime(8418): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.os.Looper.loop(Looper.java:137)
07-06 09:30:22.337: E/AndroidRuntime(8418): at android.app.ActivityThread.main(ActivityThread.java:4512)
07-06 09:30:22.337: E/AndroidRuntime(8418): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 09:30:22.337: E/AndroidRuntime(8418): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 09:30:22.337: E/AndroidRuntime(8418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-06 09:30:22.337: E/AndroidRuntime(8418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
07-06 09:30:22.337: E/AndroidRuntime(8418): at dalvik.system.NativeStart.main(Native Method)
编辑:我的新活动包含以下内容,但使用相同的 NPE 时会崩溃,但只有当我滑动以更改寻呼机视图时才会崩溃。
if (position == 0) {
View v = getLayoutInflater().inflate(R.layout.player_turn_solution_component, null);
optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_solution_component);
} else if (position == 1) {
View v = getLayoutInflater().inflate(R.layout.player_turn_clue_component, null);
optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_clue_component);
}
container.addView(optionLayout);
((ViewPager) collection).addView(container,0); //get the container ready
return container;