我已经尝试了一个月进行大量研究,将其转换为Fragment,因为我需要它用于我的导航菜单。基本上我正在创建一个应用程序。我正在使用我的MainActvity使用Fragment运行导航菜单。然后我将不得不使用这些类并将其作为Fragment运行,但有一次我必须使用AppCompatActivity,所以我将它翻译成Fragment,但遇到了Intent和其他变量的问题。我需要帮助。我只是在学习如何编码。我得到的错误是:
Error:(63, 33) error: no suitable constructor found for
Intent(Events,Class<AddEvent>) constructor Intent.Intent(String,Uri) is not
applicable (argument mismatch; Events cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable (argument
mismatch; Events cannot be converted to Context)
这是我编写的课程:
public class Events extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private FloatingActionButton mAddEventButton;
private Toolbar mToolbar;
EventCursorAdapter mCursorAdapter;
EventDbHelper eventDbHelper = new EventDbHelper(getActivity());
ListView eventListView;
ProgressDialog prgDialog;
private static final int VEHICLE_LOADER = 0;
@Nullable
@Override
public ListView onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
eventListView = (ListView) inflater.inflate(R.layout.nav_events, container,
false);
return eventListView;
mToolbar = (Toolbar) getView().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
mToolbar.setTitle("Events");
eventListView = (ListView) getView().findViewById(R.id.list);
View emptyView = getView().findViewById(R.id.empty_view);
eventListView.setEmptyView(emptyView);
mCursorAdapter = new EventCursorAdapter(getActivity(), null);
eventListView.setAdapter(mCursorAdapter);
eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long id) {
Intent intent = new Intent(Events.this, AddEvent.class, null);
Uri currentVehicleUri =
ContentUris.withAppendedId(EventContract.EventEntry.CONTENT_URI, id);
// Set the URI on the data field of the intent
intent.setData(currentVehicleUri);
startActivity(intent);
}
});
mAddEventButton = (FloatingActionButton) getView().findViewById(R.id.fab);
mAddEventButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), AddEvent.class);
startActivity(intent);
}
});
getLoaderManager().initLoader(VEHICLE_LOADER, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
EventContract.EventEntry._ID,
EventContract.EventEntry.KEY_TITLE,
EventContract.EventEntry.KEY_DATE,
EventContract.EventEntry.KEY_TIME,
EventContract.EventEntry.KEY_REPEAT,
EventContract.EventEntry.KEY_REPEAT_NO,
EventContract.EventEntry.KEY_REPEAT_TYPE,
EventContract.EventEntry.KEY_ACTIVE
};
return new CursorLoader(getActivity(), // Parent activity context
EventContract.EventEntry.CONTENT_URI, // Provider content URI to
query
projection, // Columns to include in the resulting
Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
答案 0 :(得分:0)
通过删除null修复简单:
Intent intent = new Intent(Events.this, AddEvent.class);
答案 1 :(得分:0)
问题是由这条线引起的:
Intent intent = new Intent(Events.this, AddEvent.class, null);
您应该使用可用的Public constructors之一来实例化您的意图
在这种情况下,我建议使用new Intent(view.getContext(), AddEvent.class);