片段中的拉动刷新(Chris Banes 插件)

Pull To Refresh (Chris Banes Plugin) In Fragment

我正在尝试使用拉动来刷新片段而不是列表片段。 当 pull to refresh 的行是应用程序的注释时,但是当我将它们带回来时,我收到此错误:

01-27 12:13:15.656: E/AndroidRuntime(25130): FATAL EXCEPTION: main
01-27 12:13:15.656: E/AndroidRuntime(25130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maperffecto/com.example.maperffecto.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread.access0(ActivityThread.java:151)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.os.Looper.loop(Looper.java:137)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread.main(ActivityThread.java:5293)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at java.lang.reflect.Method.invoke(Method.java:511)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at dalvik.system.NativeStart.main(Native Method)
01-27 12:13:15.656: E/AndroidRuntime(25130): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.Activity.setContentView(Activity.java:1928)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at com.example.maperffecto.MainActivity.onCreate(MainActivity.java:20)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.Activity.performCreate(Activity.java:5250)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
01-27 12:13:15.656: E/AndroidRuntime(25130):    ... 11 more
01-27 12:13:15.656: E/AndroidRuntime(25130): Caused by: java.lang.NullPointerException
01-27 12:13:15.656: E/AndroidRuntime(25130):    at com.example.maperffecto.FirstFragment.onCreateView(FirstFragment.java:52)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.Fragment.performCreateView(Fragment.java:1695)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:879)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1155)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.app.Activity.onCreateView(Activity.java:4858)
01-27 12:13:15.656: E/AndroidRuntime(25130):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
01-27 12:13:15.656: E/AndroidRuntime(25130):    ... 21 more

这是我的片段:

public class FirstFragment extends Fragment implements OnClickListener {

private Button btnByText;
private Button btnByLocation;
private onSendToSecond listener;
SimpleCursorAdapter adapter;
LocationHelper helper;
public interface onSendToSecond{
    public void sendData(String data);      
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.first_fragment_layout, container, false);
    btnByText = (Button) view.findViewById(R.id.btnByText);
    btnByLocation = (Button) view.findViewById(R.id.btnByLocation);
    btnByText.setOnClickListener(this);
    btnByLocation.setOnClickListener(this);

    helper = new LocationHelper(getActivity(), "LocationDb.db", null, 1);
    Cursor c = helper.getLocation();

    String [] from = new String [] {helper.LOCATION_COL, helper.ADDRESS_COL, helper.DISTANCE_COL};
    int [] to = new int [] {R.id.txtLocation, R.id.txtAddress, R.id.txtDistance, R.id.imglocation};

    adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_layout, c, from, to, 2);

    PullToRefreshListView list = (PullToRefreshListView) getView().findViewById(R.id.lstLocations);

    list.setAdapter(adapter);


});

    list.setAdapter(adapter);
    list.setOnRefreshListener(new OnRefreshListener<ListView>() {

        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {            
            new Handler().postDelayed(new Runnable() {

        }
    });

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    }

改变

 PullToRefreshListView list = (PullToRefreshListView) getView().findViewById(R.id.lstLocations);

 PullToRefreshListView list = (PullToRefreshListView) view.findViewById(R.id.lstLocations);

您刚刚错过了下面一行:-

ListView actualListView = mPullRefreshListView.getRefreshableView();

没有上面的线,你的下拉刷新就不起作用了。

有关详细信息,请参阅下文 link :-

https://github.com/chrisbanes/Android-PullToRefresh/blob/master/sample/src/com/handmark/pulltorefresh/samples/PullToRefreshListActivity.java