将其从AppCompatActivity转换为Fragment并具有Intent错误等 [英] Translating this from AppCompatActivity to Fragment and having Intent errors, etc

查看:1016
本文介绍了将其从AppCompatActivity转换为Fragment并具有Intent错误等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一个月进行大量研究将其转换为Fragment,因为我需要它用于我的导航菜单。基本上我正在创建一个应用程序。我正在使用我的MainActvity使用Fragment运行导航菜单。然后我将不得不使用这些类并将其作为Fragment运行,但有一次我不得不使用AppCompatActivity,所以我将它翻译成Fragment,但遇到了Intent和其他变量的问题。我需要帮助。我只是在学习如何编码。我得到的错误是:

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)

这是我编写的类:

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

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

你应该用一个可用的公共构造函数

在这种情况下,我建议使用 new Intent(view.getContext(),AddEvent.class);

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

这篇关于将其从AppCompatActivity转换为Fragment并具有Intent错误等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆