为什么我不能在片段的onCreateView中调用方法?

时间:2014-02-20 20:00:05

标签: android android-fragments

这是我第一次处理片段,所以我不知道如何使用它们。我有一个片段,我有onCreateView方法。在onCreateView方法中,我有从SQLite数据库填充自定义列表视图的代码:

@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)

所以我猜测片段在调用方法时的行为方式不同。我该怎么办?

3 个答案:

答案 0 :(得分:2)

您的Cursor C在onCreateView()的范围内定义。因此,您无法在populateListView()中使用它 你可以这样做。

private Cursor c;

现在在onCreateView:

          c = db.getAllRecordedFiles();

答案 1 :(得分:1)

Cursor c类成员和您在onCreateView内创建的本地成员是不同的对象,并重新声明您正在“隐藏”类成员的局部变量。

你可以改变

Cursor c = db.getAllRecordedFiles();

c = db.getAllRecordedFiles();

或者您可以向populateListView添加一个参数,传递Cursor对象

public void populateListView(Cursor c){
}

并从onCreateView调用它,如

 populateListView(db.getAllRecordedFiles());

答案 2 :(得分:0)

从错误中我看到你的方法抛出NPE,因为变量c没有被声明为类成员变量而是onCreateView()的局部变量。请将c作为参数传递给populateListVIew()你应该是好的。