Tabbed设计中的ListView空指针

时间:2014-10-08 21:01:29

标签: java android android-activity android-fragments parse-platform

感谢您的时间。

我正在关注https://parse.com/tutorials/parse-query-adapter并将其扩展为标签式视图。原始代码是:

public class ListFragment extends Fragment {

    private ParseQueryAdapter<ParseObject> mainAdapter;
    private ListView listView;

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

        mainAdapter = new ParseQueryAdapter<ParseObject>( this.getActivity(), "Todo" );
        mainAdapter.setTextKey("title");
        mainAdapter.setImageKey("image");

        // Initialize ListView and set initial view to mainAdapter
        listView = (ListView) rootView.findViewById(R.id.list);
        Log.d("First","Output"); // This will show on logcat
        listView.setAdapter(mainAdapter);
        Log.d("Second","Output"); // This is not shown on logcat
        mainAdapter.loadObjects();

        return rootView;
    }
}

它编译但在空指针上崩溃,似乎在 listView mainAdapter 中,我不知道发生了什么变化导致这个?

1 个答案:

答案 0 :(得分:1)

你应该这样做

listView = (ListView) rootView.findViewById(R.id.list);

因为Fragment没有findViewById方法。 http://developer.android.com/reference/android/app/Fragment.html