我已经尝试了一个月,做了很多研究,将其翻译成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); }
问题是由以下行引起的: Intent intent = new Intent(Events.this, AddEvent.class, null);
Intent intent = new Intent(Events.this, AddEvent.class, null);
您应该使用可用的Public构造函数之一实例化Intent 。 在这种情况下,我建议使用new Intent(view.getContext(), AddEvent.class);
new Intent(view.getContext(), AddEvent.class);