我试图在片段中使用pull刷新而不是列表片段。 当刷新线条是笔记时,应用程序可以正常工作,但是当我把它们带回来时,我得到了这个错误:
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);
}
答案 0 :(得分:2)
变化
PullToRefreshListView list = (PullToRefreshListView) getView().findViewById(R.id.lstLocations);
到
PullToRefreshListView list = (PullToRefreshListView) view.findViewById(R.id.lstLocations);
答案 1 :(得分:0)
你错过了下面的一行: -
ListView actualListView = mPullRefreshListView.getRefreshableView();
如果没有上线,你的刷新不会起作用。
请参阅以下链接了解更多信息: -