簡體   English   中英

如何從片段擴大視圖?

[英]How to inflate a view from a fragment?

我想在我的應用中嘗試WizarDroid庫。 但是由於我對充氣機沒有足夠的經驗,所以我在這個錯誤上停留了幾個小時。

嘗試查找ViewById時出現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電視為null。 我無法弄清楚,因為我在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致命異常:ba.imasoft.dfms.app.wizards.steps.WizardStep1.onCreateView(WizardStep1處為main java.lang.NullPointerException的.java:28)

第28行是'tv.setText(“ This'..

如果我注釋掉包含tv變量的行,那么它會起作用,而不是應該的方式。我需要為每個步驟編輯視圖。

電視=無效證明...

手表

這個問題很奇怪。

無論如何,我所做的就是將wizard.xml更改為wizard_step.xml,以為它在庫hmm中使用了wizard.xml

有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM