![](/img/trans.png)
[英]Android NullPointerException while instantiating ViewPager Fragments onCreateView but not when swyping views
[英]Android Java: Fragments returning null view in onCreateView()
我目前正在研究使用MVC設計模式和android java中的片段的程序。 我已經弄清楚了我的一個片段並使它工作,但是當我復制其他片段以遵循相同的代碼結構(具有特殊功能)時,在onCreateView方法中出現了空指針異常。
我現在在我的垃圾筆記本電腦上,它似乎無法處理android仿真,因此明天可以發布確切的錯誤代碼。 雖然我有我的源代碼,但我已經在牆上碰了很長時間,以了解它在哪里破裂。
編輯:我看到我的問題。 我正在通過從每個片段的View.java類中調用一個方法來測試我的代碼。 此方法更新視圖中的表。 由於視圖尚未顯示在屏幕上,因此尚未為其調用onCreateView()。 由於尚未調用onCreateView(),因此嘗試訪問視圖將導致空指針。 有什么好方法可以為MainActivity中的每個片段調用onCreateView(),以便我可以盡早初始化視圖?
(工作片段的一部分):
public class DispatchView extends Fragment {
private final List<DispatchModel> models = new ArrayList<DispatchModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dispatchfragment, container,
false);
return view;
}
除DispatchView之外的所有片段都會在返回視圖時中斷。 他們返回null而不是實際對象。 破碎片段之一的一部分:
public class ConnectionsLogView extends Fragment {
private final List<ConnectionsLogModel> models = new ArrayList<ConnectionsLogModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.connectionslogfragment,
container, false);
return view;
}
片段被聲明和初始化。 我嘗試將新的數據條目(類)推入它們后,它們(除Dispatch MVC之外的任何一個)都斷開了。 在我的MainActivity.java中:
public class MainActivity extends Activity {
// Declare Tab Variables and fragment objects
private mDMI app;
ActionBar.Tab Tab1, Tab2, Tab3, Tab4;
Fragment dispatchTab = new DispatchView();
Fragment dispatchLogTab = new DispatchLogView();
Fragment activeConnectionsTab = new ConnectionsView();
Fragment connectionLogTab = new ConnectionsLogView();
DispatchModel dispatchModel;
DispatchController dispatchController;
DispatchLogModel dispatchLogModel;
DispatchLogController dispatchLogController;
ConnectionsModel connectionsModel;
ConnectionsController connectionsController;
ConnectionsLogModel connectionsLogModel;
ConnectionsLogController connectionsLogController;
public MainActivity() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (mDMI) getApplication();
dispatchModel = app.getDispatchModel();
dispatchController = new DispatchController(dispatchTab, dispatchModel);
dispatchLogModel = app.getDispatchLogModel();
dispatchLogController = new DispatchLogController(dispatchLogTab,
dispatchLogModel);
connectionsModel = app.getConnectionsModel();
connectionsController = new ConnectionsController(activeConnectionsTab,
connectionsModel);
connectionsLogModel = app.getConnLogModel();
connectionsLogController = new ConnectionsLogController(
connectionLogTab, connectionsLogModel);
setContentView(R.layout.activity_main);
xml字符串在我的R.java中標識:
public static final class layout {
public static final int activity_login=0x7f030000;
public static final int activity_main=0x7f030001;
public static final int connectionsfragment=0x7f030002;
public static final int connectionslogfragment=0x7f030003;
public static final int dispatchfragment=0x7f030004;
public static final int dispatchlogfragment=0x7f030005;
}
不要以這種方式創建片段。 而是使用標准的Android模式:
public class FeedFragment extends Fragment {
public FeedFragment() {
super();
}
public static FeedFragment newInstance(Context context) {
if (context != null) {
sContext = context.getApplicationContext();
} else {
sContext = YourApp.getInstance().getApplicationContext();
}
return new FeedFragment();
}
}
然后不要在您的活動中像那樣創建它們…
而是在Activity中使用標准的Android模式:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.content_frame);
if ( fragment == null ) {
fragment = FeedFragment.newInstance(this);
fm.beginTransaction()
.add(R.id.content_frame, dispatchTab, "DISPATCH_FRAG")
.commit();
}
}
要稍后檢索片段,您可以...
final FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag("DISPATCH_FRAG);
if (fragment != null) {
// cast and use your fragment
}
如果以后需要,甚至可以存儲參考。
關於您的null問題,如果沒有確切的崩潰/日志,很難說出來。
但是您的代碼有點混亂,這可能是原因。 片段生命周期非常棘手。
我自己修好了。 如果該視圖返回null,則我的更新函數不會嘗試向(不存在)視圖中的(不存在)表添加行和條目。 我在每個視圖中都這樣實現了:
public void update(final BlockingDeque<Dispatch> dispatches) {
View view = getView();
// do not update if the view is null (device is not displaying that
// fragment)
if (view != null) {
TableLayout t = (TableLayout) view.findViewById(R.id.dispatchTable);
t.removeAllViews();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.