我發現下面的代碼放在一個項目(變量/類名稱更改):參數是否可以像這樣傳遞給片段?
private FragmentParams params;
public static MyFragment newInstance(FragmentParams params) {
MyFragment fragment = new MyFragment();
fragment.params = params; // <- Manually setting params in fragment class
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
ButterKnife.bind(this, view);
/* The following line initializes a class responsible for the user
interface. The params are used for that purpose. However, out of
nowhere, the params where null 2 times.*/
presenter.setParams(params);
return view;
}
該項目已住了幾個星期和一對夫婦NullPointerException
報道當presenter
代碼訪問的PARAMS(進一步)。
我知道正確的方法是使用setArguments()
,但我想問的問題是:可以指出的線有問題嗎?
我能想到的唯一的東西,那Android的出於某種原因重新創建的片段和params
成爲null
由於方法newInstance
不會被調用,因此沒有設置params
。
不可以。你必須把它傳遞給'Bundle'。 – Piyush
也許你是在代碼的某個地方實例化類,如'Fragment()',而不是'newInstance',那裏的參數是'null' –
@Piyush你能讀一下最後一段,告訴我你對我懷疑的看法? – JonZarate