Translating this from AppCompatActivity to Fragment and having Intent errors, etc

穿精又带淫゛_ 提交于 2019-12-20 07:42:38

问题


I have been trying for a month doing a lot of research to translate this to Fragment since I need it for my navigation Menu. Basically I'm creating an app. I'm using my MainActvity to run the navigation menu using Fragment. Then I would have to use the classes and run it as Fragment, but at one point I had to use AppCompatActivity, so I translated it to Fragment, but came across a problem with Intent, and other variables. I need help. I'm just learning how to code. The errors I'm getting are:

 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)

Here is the class I coded:

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);

}

回答1:


The problem is caused by this line:
Intent intent = new Intent(Events.this, AddEvent.class, null);

You should instantiate your Intent with one of the available Public constructors.
In this case, I would suggest using new Intent(view.getContext(), AddEvent.class);




回答2:


Fix simple by removing null:

Intent intent = new Intent(Events.this, AddEvent.class);


来源:https://stackoverflow.com/questions/50496105/translating-this-from-appcompatactivity-to-fragment-and-having-intent-errors-et

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!