我正在使用滑动标签创建地址簿应用程序,即" ViewPager"。当我运行应用程序时,它崩溃了。我收到错误,我不明白。请有人可以帮助我吗?感谢。
这是我的Fragm.java Fragment类。
package com.rufflez.viewpagerexample;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Fragm extends Fragment{
private CursorAdapter mAdapter;
public ListView listView;
public Cursor cursor;
public static final String[] FROM = { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
private static final int[] TO = { R.id.text_name, R.id.contact_thumbnail };
int layout = R.layout.contact_list_item;
private android.content.Context context = getActivity();
public View view;
public static Uri uri;
// columns requested from the database
private static final String[] PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.IN_VISIBLE_GROUP
};
// this goes in the CursorLoader parameter list, it filters
// out only those contacts who have a phone number
private static final String SELECTION =
ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
// Empty public constructor, required by the system
public Fragm() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.contact_list_view,
container, true);
listView = (ListView) view.findViewById(R.id.list);
Cursor c = null; // there is no cursor yet
int flags = 0;
mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
return view;
}
}
这是我的TestFragmentAdapter.java类
package com.rufflez.viewpagerexample;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import com.viewpagerindicator.IconPagerAdapter;
public class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
protected static final String[] CONTENT = new String[] {
"This", "Is", "A", "Test"
};
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment1();
switch(position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragm();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
}
return fragment;
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position){
String title = "";
switch(position){
case 0:
title = "Fragment 1";
break;
case 1:
title = "Fragment 2";
break;
case 2:
title = "Fragment 3";
break;
case 3:
title = "Fragment 4";
break;
}
return title;
}
public void setCount(int count){
if (count > 0 && count < 10){
mCount = count;
notifyDataSetChanged();
}
}
}
这是我的Logcat错误
08-31 21:42:57.956: E/AndroidRuntime(6614): Process: com.rufflez.viewpagerexample, PID: 6614
08-31 21:42:57.956: E/AndroidRuntime(6614): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:95)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
08-31 21:42:57.956: E/AndroidRuntime(6614): at com.rufflez.viewpagerexample.Fragm.onCreateView(Fragm.java:55)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.View.measure(View.java:18580)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.widget.LinearLayout.measureVertical(LinearLayout.java:874)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.View.measure(View.java:18580)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.View.measure(View.java:18580)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
08-31 21:42:57.956: E/AndroidRuntime(6614): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.View.measure(View.java:18580)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-31 21:42:57.956: E/AndroidRuntime(6614): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.View.measure(View.java:18580)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
08-31 21:42:57.956: E/AndroidRuntime(6614): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
答案 0 :(得分:0)
您的context
为空。您需要在onAttach
之后填充它。一种可能的解决方案是从onCreateView
方法中设置它。
以下是有关片段和活动生命周期的一些信息的链接:https://github.com/xxv/android-lifecycle
片段上的 getActivity()
只会返回onAttach
和onDetach
之间的活动
答案 1 :(得分:0)
由于初始化上下文的方式,您得到一个空指针:
this
以下内容适用于某项活动:
private Context context = getActivity();
但是对于一个片段,它将在周期的早期,所以你需要在片段附加后进行。
private Context context = this;
是一个很好的地方:
OnCreate()