Android 片段中的拉刷新(Chris Banes插件)
我试图在片段中使用pull刷新,但不是list片段。 当pull to refresh的行是注释时,应用程序工作,但当我将它们带回来时,我得到以下错误:Android 片段中的拉刷新(Chris Banes插件),android,android-fragments,eclipse-plugin,pull-to-refresh,Android,Android Fragments,Eclipse Plugin,Pull To Refresh,我试图在片段中使用pull刷新,但不是list片段。 当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 Componen
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.access$700(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);
}
public类FirstFragment扩展了片段实现OnClickListener{
私人按钮btnByText;
专用按钮BTN定位;
私有的onSendToSecond侦听器;
SimpleCursorAdapter适配器;
位置助手;
公共接口onSendToSecond{
公共数据(字符串数据);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.first\u fragment\u布局,容器,错误);
btnByText=(按钮)view.findViewById(R.id.btnByText);
btnByLocation=(按钮)view.findViewById(R.id.btnByLocation);
btnByText.setOnClickListener(这个);
setOnClickListener(this);
helper=newlocationhelper(getActivity(),“LocationDb.db”,null,1);
游标c=helper.getLocation();
字符串[]from=新字符串[]{helper.LOCATION\u COL,helper.ADDRESS\u COL,helper.DISTANCE\u COL};
int[]to=newint[]{R.id.txtLocation,R.id.txtdaddress,R.id.txtdestance,R.id.imglocation};
adapter=new SimpleCursorAdapter(getActivity(),R.layout.list_layout,c,from,to,2);
PullToRefreshListView列表=(PullToRefreshListView)getView().findViewById(R.id.lstLocations);
list.setAdapter(适配器);
});
list.setAdapter(适配器);
setOnRefreshListener(新的OnRefreshListener(){
@凌驾
public void onRefresh(PullToRefreshBase refreshView){
new Handler().postDelayed(new Runnable()){
}
});
返回视图;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
}
更改
PullToRefreshListView list = (PullToRefreshListView) getView().findViewById(R.id.lstLocations);
到
您刚刚漏掉了下面一行:-
ListView actualListView = mPullRefreshListView.getRefreshableView();
没有以上的线,你拉刷新不工作
有关更多信息,请参阅以下链接:-
也发布
first\u fragment\u layout.xml
文件除了您的错误:您可以使用基于Chris Banes abbadoned lib创建的文件。有一些修复和新功能,非常有用
ListView actualListView = mPullRefreshListView.getRefreshableView();