簡體   English   中英

創建片段視圖后有什么方法嗎?如何在創建片段后更改選項卡的布局

[英]Is there any method after creating the view of fragments?How to change Layout of tabs after creating fragments

我需要調用一個方法,在該方法中我解析JSON對象並設置值,然后在TextField和按鈕值中設置值,然后創建了片段,但是在創建了片段之后,如何更改文本字段值

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        //Switch to different layout accordingly
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1: {
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
            }
            case 2: {
                rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
                break;
            }

            case 3: {
                rootView = inflater.inflate(R.layout.fragment_totals, container, false);
                break;
            }
            case 4: {
                rootView = inflater.inflate(R.layout.fragment_keyboard, container, false);
                break;
            }
            case 5: {
                rootView = inflater.inflate(R.layout.fragment_keylock, container, false);
                break;
            }

        }
        return rootView;
    }
}

我嘗試在onStart調用該方法,但是在onCreateView之前先調用onStart方法,因此在分配值時會出現空指針異常:

@Override
  public void onStart() {
    super.onStart();
    parseJsonResponse(response_message);
  }

在parseJsonResponse()方法中,我設置了值

String UIFooter = ResponseHeaderObject.getString("FOOTER");
//Set Header Display
headerTextView =(TextView) findViewById(R.id.headerTextView);
headerTextView.setText(UIHeader);

這是來自logcat的錯誤:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.mpos.kits.smartpos.PosMainActivity.parseJsonResponse(PosMainActivity.java:366)

片段具有一個名為: onViewCreated的函數,該函數在創建視圖后調用

是的,該方法存在: onViewCreated 但是,如果要在創建文本的方法之外將文本分配給TextView ,則必須將TextView聲明為成員屬性(在任何方法之外),以允許所有類方法使用它。 否則,在一個片段中,將很難操作它。

由以下原因引起:java.lang.NullPointerException:嘗試在com.mpos.kits.smartpos.PosMainActivity.parseJsonResponse(PosMainActivity(PosMainActivity) .java:366)

您收到此錯誤,因為此headerTextView =(TextView) findViewById(R.id.headerTextView); 除非在onCreateView方法中引用了根視圖,否則在片段中返回null 這樣(TextView) rootView.findViewById(R.id.section_label);

如果要覆蓋onViewCreated方法,請按照以下方式進行操作:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

        //your code
    }

暫無
暫無

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

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