2014-02-20 191 views
-1

這是我第一次處理碎片,所以我不知道如何操作它們。我有一個片段,我有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在調用方法時行爲不同。我該怎麼辦?

+0

作爲開發者,我們應該關心準確性。 Java沒有功能。你已經寫了一個方法。 – Simon

+0

感謝您的更正 – Guy

回答

1

Cursor c類成員和您在onCreateView中創建的本地對象是不同的對象,並且重新聲明瞭一個局部變量,以「隱藏」類成員。

你可以改變

Cursor c = db.getAllRecordedFiles(); 

c = db.getAllRecordedFiles(); 

,或者你可以添加一個參數populateListView傳遞Cursor對象

public void populateListView(Cursor c){ 
} 

和onCreateView這樣稱呼它

populateListView(db.getAllRecordedFiles()); 
+0

非常感謝! – Guy

2

您的光標C在onCreateView()的範圍內定義。所以你不能在populateListView() 中使用它你可以做這樣的事情。

private Cursor c; 
現在

在onCreateView:

  c = db.getAllRecordedFiles(); 
0

從我看到你的方法拋出NPE因變量c不聲明爲類的成員變量,而局部變量onCreateView()的錯誤。請將c作爲參數傳遞給populateListVIew(),您應該很好。

相關問題