我在运行时遇到空指针异常。我从PagerView的2个XML文件中提取RelativeLayouts,我似乎搞砸了。知道导致错误的是什么吗?
感谢您的帮助!
我有两个具有相同结构的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;
答案 0 :(得分:1)
你需要这样的东西
View v = getLayoutInflater().inflate(R.layout.layout_taht_have_player_turn_solution_component, null);
和
if (position == 0) {
optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_solution_component);
optionCaption = (TextView) v.findViewById(R.id.solutionCaption);
}