Fragment帶引數建構函式時報錯解決方法
阿新 • • 發佈:2019-01-27
報錯資訊如下:
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ***.base.OlineFragment
: make sure class name exists, is public, and has an empty constructor that is public....
這個時候正確的方法是例項化。
public static OnlineFragment getInstance(int id) { OnlineFragment instance = new在onCreateView的方法裡面再取出來傳進引數。OnlineFragment(); Bundle args = new Bundle(); args.putInt("id", id); instance.setArguments(args); return instance; }
Bundle args = getArguments(); if (args != null) { id = args.getInt("id"); }