错误
initLoader(int,android.os.Bundle,android.support.v4.app.LoaderManager.LoaderCallbacks)'在' android.support.v4.app.LoaderManager'不能应用于'(int,null,group15.cop4331project.MyReportsFragment)
我已经阅读了有关支持库的所有答案,但事实并非如此。我试图使用SQLite数据库中的列表填充此片段。我真的想弄清楚为什么这不起作用。另外,我认为我正确使用OnCreateView和OnActivityCreated,但我不确定。
我的导入
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
我的代码
public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks < Cursor > {
//adapter for the ListView
MyCursorAdapter mCursorAdapter;
View rootView;
public MyReportsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.report_list, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Find the listview which will be populated with the data
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Setup an Adapter to create a list item for each row of data in the Cursor.
mCursorAdapter = new ReportCursorAdapter(getActivity(), null);
listView.setAdapter(mCursorAdapter);
// Setup the item click listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
public void onItemClick(AdapterView < ?>adapterView, View view, int position, long id) {
Intent intent = new Intent(getActivity(), EditReportActivity.class);
Uri currentUri = ContentUris.withAppendedId(ReportEntry.CONTENT_URI, id);
// Set the URI on the data field of the intent
intent.setData(currentUri);
startActivity(intent);
}
});
// Kick off the loader
getLoaderManager().initLoader(REPORT_LOADER, null, this);
//getActivity().getSupportLoaderManager().initLoader(REPORT_LOADER, null, this);
}
}
答案 0 :(得分:0)
应该在onActivityCreated中调用initLoader,以便部分正常。
我遇到了一个类似的问题,solotion与支持v4 lib有关。更改此导入语句:
import android.support.v4.app.Fragment;
至:
import android.app.Fragment;
并使用:import android.app.LoaderManager;
(确保您没有在项目中的任何位置使用支持v4)
这是基于您发布的代码,如果您可以发布所有可能有用的imort语句
希望这会有所帮助