最後更新片段標籤VS務實添加片段佈局
證明,我有問題都錯了。這是我使用Fragment標籤的方式。請參閱下面的答案以獲得完整的解釋。
更新3: 所以我繼續搞了幾個小時,沒有這樣的運氣。我已經能夠達到它不會崩潰的程度。但現在我相信我知道這個問題可能是什麼。它讓我的calendarAdapter
爲空,因爲它是空的,但不是我的意圖。看起來onCreateView
正在運行兩次,一次顯然在發送任何東西之前以及在我正式發送了一些參數之後。這當然不應該發生......我想。我將提供將ActionBar設置在底部的部分,以便讓我知道如果我正在做出錯誤的行爲來設置它的行爲方式。
由於某種原因,我遇到了這個特定問題,我不知道爲什麼。我有一個名爲MainDisplayActivity
的活動以及一個名爲MainDislpayFragment
的片段。正如你可以想象的那樣,Activity在app start上設置了Fragment。現在,這是問題,我一直試圖發送作爲參數到newInstance
構造函數的三個數字來自一個日曆。然後我從構造函數中創建了日曆。但我的應用程序崩潰。奇怪的部分是當我讓日曆不被靜態字段使用時,應用程序按預期工作,但試圖從構造函數中使用日曆來使其崩潰。這是一個例子方法:
private static Calendar cal;
static MainDisplayFragment newInstance(Context context, int y, int m, int d) {
c = context;
MainDisplayFragment f = new MainDisplayFragment();
//static global Calendar now
cal = Calendar.getInstance();
cal.set(y, m, d);
}//end of static newInstance method
現在,它重要的是要注意,即使我嘗試服用日曆出來,並把它的onCreate,只具有靜態的整數,我又回到全球,並設定他們那裏,仍然導致崩潰。值得注意的錯誤和奇怪的是,它似乎認爲日曆由於某種原因而爲空。正如你可以告訴那裏,它肯定是被創造出來的。但現在我開始認爲日曆和靜態字段存在問題。如果沒有,那麼我可能會有一個錯誤,但如果可以的話,我想排除這一點。感謝任何人的迴應。
更新:我應該提及,我使用該日曆信息無非是傳入我的MyCalendarAdapter
。無論我只是將數字作爲參數還是日曆本身發送,它都會崩潰。我看到的第一個空例外是我試圖設置MyCalendarAdapter
的地方。
75號線將是最後林在這裏:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.main_fragment, container, false);
calendarAdapter = new MyCalendarAdapter(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
更新2:
更亂搞,我意識到這個問題是MyCalendarAdapter
後,但它的問題,使沒有意義。看起來,如果你實例化一個沒有參數的calendarAdapter,它可以完美的工作,但是它的參數會崩潰。這些是MyCalendarAdapter
的兩個構造函數,所以人們可以看到沒有看起來錯誤,這使得這更糟糕,因爲我沒有friggen的想法如何解決這個問題。知道我試圖移動到片段的代碼最初來自一個完美運行的Activity,這一點也很重要。
public MyCalendarAdapter() {
thisMonth = Calendar.getInstance();
now = new MonthDisplayHelper(thisMonth.get(Calendar.YEAR), thisMonth.get(Calendar.MONTH));//creating Helper for month display
currentDay = thisMonth.get(Calendar.DAY_OF_MONTH);//sets current day
...
}
public MyCalendarAdapter(int year, int month, int currentDay) {
thisMonth = Calendar.getInstance();
now = new MonthDisplayHelper(year, month);
this.currentDay = currentDay;
...
}
完全錯誤列表:
其中片段被設置
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(this, "Position is "+itemPosition, Toast.LENGTH_SHORT).show();
if(itemPosition == 0) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
//check to see if one already exists and remove it
Fragment prev = getFragmentManager().findFragmentByTag(MAIN_FRAGMENT);
if(prev!=null) {
ft.remove(prev);
Toast.makeText(this, "Fragment was found", Toast.LENGTH_SHORT).show();
}
MyCalendarAdapter calendarAdapter = new MyCalendarAdapter(year, month, day);
//Log.d("MAINDISPLAYACTIVTY", "calendarAdapter year is "+calendarAdapter.getCalendar().getYear());
currentFragment = MainDisplayFragment.newInstance(this, year, month, day, calendarAdapter);
ft.add((MainDisplayFragment)currentFragment, MAIN_FRAGMENT);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//ft.addToBackStack(null); //i do not want this behavior
ft.commit();
return true;
}
... continues on with the other cases
的onCreate在活動
11-08 18:26:35.710: E/AndroidRuntime(7839): FATAL EXCEPTION: main 11-08 18:26:35.710: E/AndroidRuntime(7839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zeroe/com.zeroe.MainDisplayActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread.access$600(ActivityThread.java:130) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.os.Handler.dispatchMessage(Handler.java:99) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.os.Looper.loop(Looper.java:137) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread.main(ActivityThread.java:4745) 11-08 18:26:35.710: E/AndroidRuntime(7839): at java.lang.reflect.Method.invokeNative(Native Method) 11-08 18:26:35.710: E/AndroidRuntime(7839): at java.lang.reflect.Method.invoke(Method.java:511) 11-08 18:26:35.710: E/AndroidRuntime(7839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 11-08 18:26:35.710: E/AndroidRuntime(7839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-08 18:26:35.710: E/AndroidRuntime(7839): at dalvik.system.NativeStart.main(Native Method) 11-08 18:26:35.710: E/AndroidRuntime(7839): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 11-08 18:26:35.710: E/AndroidRuntime(7839): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.Activity.setContentView(Activity.java:1867) 11-08 18:26:35.710: E/AndroidRuntime(7839): at com.zeroe.MainDisplayActivity.onCreate(MainDisplayActivity.java:60) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.Activity.performCreate(Activity.java:5008) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 11-08 18:26:35.710: E/AndroidRuntime(7839): ... 11 more 11-08 18:26:35.710: E/AndroidRuntime(7839): Caused by: java.lang.NullPointerException 11-08 18:26:35.710: E/AndroidRuntime(7839): at com.zeroe.MainDisplayFragment.onCreateView(MainDisplayFragment.java:80) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.app.Activity.onCreateView(Activity.java:4664) 11-08 18:26:35.710: E/AndroidRuntime(7839): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 11-08 18:26:35.710: E/AndroidRuntime(7839): ... 21 more
動作條部,其設定在ActionBar
final ActionBar bar = getActionBar();
bar.setDisplayShowTitleEnabled(false);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
bar.setListNavigationCallbacks(new ArrayAdapter<String>(bar.getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1,
new String[]{"MONTH", "WEEK", "DAY"}), this);
if(savedInstanceState == null) {
bar.setSelectedNavigationItem(0);
} else {
bar.setSelectedNavigationItem(savedInstanceState.getInt(SELECTED_NAVIGATION_STATE));
}
沒有問題在那裏實例化日曆。你能告訴我們日曆靜態字段聲明,你如何訪問它和NPE的堆棧跟蹤? – m0skit0
@ m0skit0增加了一些信息。至少相關種類 – Andy
什麼是currentDate,它在哪裏設置?這在以上代碼示例中不可見 – JRaymond