我正在使用ViewPager,其中包含一个RecyclerView,该RecyclerView位于ViewPager的第一个片段中。
当我从左向右滚动时,没有错误。但是当我从右向左滚动(从ViewPager的#2到#1片段),会出现以下错误:
这是错误信息: java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()'
下面的方法是我的ViewPager中的第一个片段。当我注释掉调用RecyclerView的那几行时,就不会出现错误。但是我需要这个RecyclerView来浏览我的联系人!所以我猜测错误发生在那些代码行中...
以下是这些行: // 设置列表。
我在谷歌上进行了很多研究,但是我找不到解决这些错误的方法。
有人猜测吗?
当我从左向右滚动时,没有错误。但是当我从右向左滚动(从ViewPager的#2到#1片段),会出现以下错误:
05-10 20:21:07.546 20002-20002/com.graffiti75.android.worldremit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.graffiti75.android.worldremit, PID: 20002
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference
at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1819)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
at android.view.View.dispatchPointerEvent(View.java:8666)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-10 20:21:07.549 550-7690/? W/ActivityManager﹕ Force finishing activity 1 com.graffiti75.android.worldremit/.activity.WalkthroughActivity
这是错误信息: java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()'
下面的方法是我的ViewPager中的第一个片段。当我注释掉调用RecyclerView的那几行时,就不会出现错误。但是我需要这个RecyclerView来浏览我的联系人!所以我猜测错误发生在那些代码行中...
public class ContactListFragment extends Fragment {
//--------------------------------------------------
// Constructor
//--------------------------------------------------
public static ContactListFragment newInstance() {
ContactListFragment fragment = new ContactListFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public ContactListFragment() {
// Required empty public constructor
}
//--------------------------------------------------
// Fragment Life Cycle
//--------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment.
View v = inflater.inflate(R.layout.fragment_contact_list, container, false);
// Get data.
List<String> nameList = new ArrayList<String>();
Cursor cursor = getContacts();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
nameList.add(name);
}
// Setup list.
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.id_recycler_view);
recyclerView.setHasFixedSize(true);
Activity activity = getActivity();
final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(new ContactListAdapter(getActivity(), nameList));
return v;
}
//--------------------------------------------------
// Methods
//--------------------------------------------------
private Cursor getContacts() {
// Run query.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
String[] selectionArgs = null;
//String sortOrder = ContactsContract.Contacts._ID + " COLLATE LOCALIZED ASC";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return getActivity().managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
}
以下是这些行: // 设置列表。
我在谷歌上进行了很多研究,但是我找不到解决这些错误的方法。
有人猜测吗?