如何将其从AppCompatActivity转换为Fragment

时间:2018-05-18 20:26:23

标签: java android android-fragments

我已经尝试了一个月进行大量研究,将其转换为Fragment,因为我需要它用于我的导航菜单。基本上我是在创建一个应用程序。我使用我的MainActvity使用Fragment运行导航菜单。然后我将不得不使用这些类并将其作为Fragment运行,但有一次我不得不使用AppCompatActivity,因此我将其转换为Fragment,但遇到了Intent和其他变量的问题。

这是MainActivity

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {




override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)


    val toggle = ActionBarDrawerToggle(
            this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()

    nav_view.setNavigationItemSelectedListener(this)
}

override fun onBackPressed() {
    if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
        drawer_layout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    when (item.itemId) {
        R.id.action_refresh -> return true
        else -> return super.onOptionsItemSelected(item)
    }
}



override fun onNavigationItemSelected(item: MenuItem): Boolean {
    val fragmentManager = fragmentManager
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.content_main -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , MainContent())
                    .commit()
            // Handle the camera action
        }
        R.id.nav_events -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Events())
                    .commit()

        }
        R.id.nav_map -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Map())
                    .commit()

        }
        R.id.nav_study -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Study())
                    .commit()

        }
        R.id.nav_avatar -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Avatar())
                    .commit()

        }
        R.id.nav_settings -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Settings())
                    .commit()

        }
        R.id.nav_faq -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,FAQ())
                    .commit()

        }
        R.id.nav_share -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,ShareCalendar())
                    .commit()

        }
        R.id.nav_send -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,SendEvent())
                    .commit()

        }
        R.id.nav_feedback -> {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            ,Feedback())
                    .commit()

        }
    }

    drawer_layout.closeDrawer(GravityCompat.START)
    return true
}

}

这是Events类

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

}

}

我需要帮助。我正在学习如何编码。 我得到的错误是:

我带走了#34; null&#34; from line&#34; Intent intent = new Intent(Events.this,AddEvent.class,null);&#34;在Events类中出现此错误:

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)

0 个答案:

没有答案