我想在我的应用中试用WizarDroid库。但由于我对充气器相当缺乏经验,因此我坚持这个错误几个小时。
我在尝试 findViewById 时遇到 NullPointerException ,我无法弄清楚原因。
public class WizardStep1 extends WizardStep {
//Wire the layout to the step
public WizardStep1() {
}
//Set your layout here
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.wizard, container, false);
TextView tv = (TextView) v.findViewById(R.id.wizard_textView); // tv = null
tv.setText("This is an example of Step 1 in the wizard. Press the Next " + // NullPointerException gets thrown here
"button to proceed to the next step. Hit the back button to go back to the calling activity.");
return v;
}
}
所以 TextView tv 由于某种原因为空。我无法理解这一点,因为我在XML R.layout.wizard
中明确定义了它<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/wizard_textView"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:text="gtsets" />
</LinearLayout>
此代码也来自他们的文档,因此它让我更加困惑:/
编辑: 这是wizardStartup类:
public class wizardStartup extends BasicWizardLayout {
/**
* Note that initially BasicWizardLayout inherits from {@link android.support.v4.app.Fragment} and therefore you must have an empty constructor
*/
public wizardStartup() {
super();
}
//You must override this method and create a wizard flow by
//using WizardFlow.Builder as shown in this example
@Override
public WizardFlow onSetup() {
/* Optionally, you can set different labels for the control buttons
setNextButtonLabel("Advance");
setBackButtonLabel("Return");
setFinishButtonLabel("Finalize"); */
return new WizardFlow.Builder()
.addStep(WizardStep1.class) //Add your steps in the order you want them
.addStep(WizardStep2.class) //to appear and eventually call create()
.create(); //to create the wizard flow.
}
}
LogCat:
07-15 17:58:05.491 10299-10299 / ba.imasoft.dfms.app E / AndroidRuntime: 致命异议:主要 显示java.lang.NullPointerException at ba.imasoft.dfms.app.wizards.steps.WizardStep1.onCreateView(WizardStep1.java:28)
第28行是'tv.setText(“This'..
如果我注释掉包含tv变量的行它可以工作但不是它应该的方式..我需要编辑每个步骤的视图。
tv = null证明......
答案 0 :(得分:0)
这个问题很奇怪......
无论如何,我所做的就是将 wizard.xml 更改为 wizard_step.xml ,认为它正在使用 wizard.xml 作为库中的其他内容HMM ..
有效。