這是我第一次處理碎片,所以我不知道如何操作它們。我有一個片段,我有onCreateView方法。在那onCreateView方法我有從SQLite數據庫填充自定義列表視圖的代碼:爲什麼我不能在fragment的onCreateView中調用方法?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_library, container, false);
Log.i("TAG", "onCreateView");
// Variables
lView = (ListView) v.findViewById(R.id.listView);
components = new ArrayList<RecordDetails>();
db = new DatabaseHandler(getActivity());
Cursor c = db.getAllRecordedFiles();
if(c.moveToFirst()){
element = new RecordDetails();
element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
components.add(element);
while (c.moveToNext())
{
element = new RecordDetails();
element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
components.add(element);
}
lView.setAdapter(new CustomListViewAdapter(components, getActivity()));
}
return v;
}
這工作正常。現在,讓我們說,我希望把代碼這樣的移植ListView進入方法和然後調用該方法在onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_library, container, false);
Log.i("TAG", "onCreateView");
// Variables
lView = (ListView) v.findViewById(R.id.listView);
components = new ArrayList<RecordDetails>();
db = new DatabaseHandler(getActivity());
Cursor c = db.getAllRecordedFiles();
populateListView();
return v;
}
public void populateListView(){
if(c.moveToFirst()){
element = new RecordDetails();
element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
components.add(element);
while (c.moveToNext())
{
element = new RecordDetails();
element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
components.add(element);
}
lView.setAdapter(new CustomListViewAdapter(components, getActivity()));
}
}
當我這樣做,應用程序崩潰與下面的logcat的輸出:
02-20 20:57:17.505: E/AndroidRuntime(4675): FATAL EXCEPTION: main
02-20 20:57:17.505: E/AndroidRuntime(4675): java.lang.NullPointerException
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.matejhacin.beautifulvoicerecorder.LibraryFragment.populateListView(LibraryFragment.java:70)
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.matejhacin.beautifulvoicerecorder.LibraryFragment.onCreateView(LibraryFragment.java:64)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.View.measure(View.java:15855)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.View.measure(View.java:15855)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.View.measure(View.java:15855)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2451)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.View.measure(View.java:15855)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.Choreographer.doFrame(Choreographer.java:532)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.os.Handler.handleCallback(Handler.java:730)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.os.Looper.loop(Looper.java:137)
02-20 20:57:17.505: E/AndroidRuntime(4675): at android.app.ActivityThread.main(ActivityThread.java:5289)
02-20 20:57:17.505: E/AndroidRuntime(4675): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 20:57:17.505: E/AndroidRuntime(4675): at java.lang.reflect.Method.invoke(Method.java:525)
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
02-20 20:57:17.505: E/AndroidRuntime(4675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
02-20 20:57:17.505: E/AndroidRuntime(4675): at dalvik.system.NativeStart.main(Native Method)
所以我猜Fragments在調用方法時行爲不同。我該怎麼辦?
作爲開發者,我們應該關心準確性。 Java沒有功能。你已經寫了一個方法。 – Simon
感謝您的更正 – Guy