在FirebaseListAdapter(Android)中调用上下文

时间:2016-02-06 05:11:22

标签: android firebase fragment

我创建了一个包含三个片段的标签式活动。每当我滚动到使用FirebaseListAdapter的ListFragment时,我都会收到NullPointerException。我怀疑它与FirebaseListAdapter中的context参数有关。我在其他帖子中读到getActivity()应该在Fragments上使用,但它在我的案例中没有用。

这是我的片段类捕获错误:

public class ActionPlan extends ListFragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_action_plan, container, false);
    getActionPlan(rootView);
    return rootView;
}



public void getActionPlan(final View view){

    Firebase ref = new Firebase("https://infotrack.firebaseio.com/infotrack/rso/actionplan");

    ListView ap = (ListView) view.findViewById(R.id.list);

    FirebaseListAdapter<ApContent> apAdapter = new FirebaseListAdapter<ApContent>(getActivity(), ApContent.class, R.layout.layout_action_plan, ref) {
        @Override
        protected void populateView(View view, ApContent content, int position) {

            ((TextView) view.findViewById(R.id.txtActivity)).setText(content.getActivity());
            ((TextView) view.findViewById(R.id.txtTimeframe)).setText("TIMEFRAME: " +content.getTimeframe());
            ((TextView) view.findViewById(R.id.txtCorevalues)).setText("CORE VALUES: " +content.getCorevalues());
            ((TextView) view.findViewById(R.id.txtObjective)).setText("OBJECTIVE: " +content.getObjective());
            ((TextView) view.findViewById(R.id.txtStrategy)).setText("STRATEGY: " +content.getStrategy());
            ((TextView) view.findViewById(R.id.txtManpower)).setText("MANPOWER: " +content.getManpower());
            ((TextView) view.findViewById(R.id.txtBudget)).setText("BUDGET PROPOSAL: " +content.getBudget());

        }
    };

    ap.setAdapter(apAdapter);       
}

}

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main

  java.lang.NullPointerException
      at xyz.kimfeliciano.infotrack.infotrack.ActionPlan.getActionPlan(ActionPlan.java:47)
      at xyz.kimfeliciano.infotrack.infotrack.ActionPlan.onCreateView(ActionPlan.java:20)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
      at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
      at android.view.Choreographer.doCallbacks(Choreographer.java:555)
      at android.view.Choreographer.doFrame(Choreographer.java:524)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
      at android.os.Handler.handleCallback(Handler.java:615)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:213)
      at android.app.ActivityThread.main(ActivityThread.java:4787)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
      at dalvik.system.NativeStart.main(Native Method)

这些行的错误点:ap.setAdapter(apAdapter);getActionPlan(rootView);

1 个答案:

答案 0 :(得分:1)

似乎在当前布局中找不到R.id.list。你确定它在那里吗?