Я новичок в разработке приложений для Android и сейчас изучаю концепцию фрагментов. Я создал операцию контейнера фрагментов (Home_page.java), которая по умолчанию раздувает Home_pageFragment. Макет Home_pageFragment состоит из EditText и кнопки. При нажатии кнопки Home_pageFragment следует заменить на Menu_pageFragment в том же контейнере активности. Я написал код для замены предыдущего фрагмента на новый фрагмент.Ошибка при замене фрагмента в контейнере
Все компилирует за исключением ниже линии в Home_pageFragment.java
transaction.replace(R.id.fragment_container, newFragment);
, который показывает ошибку под словом «newfragment», говоря:
Wrong 2nd argument type found 'com.technology.computer.mit.ctechmit.Menu_pageFragment' required 'Android.app.Fragment'
Может кто-то пожалуйста предложить мне решение это?
Ниже мой Home_pageFragment.java, в котором метод SendMessage() заменяет его Menu_pageFragment по нажатию кнопки посыла в его расположение:
package com.technology.computer.mit.ctechmit;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class Home_pageFragment extends Fragment {
public void sendMessage(View view) {
// Do something in response to button
// Create fragment and give it an argument specifying the article it should show
Menu_pageFragment newFragment = new Menu_pageFragment();
Bundle args = new Bundle();
//args.putInt(Menu_pageFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_page, container, false);
}
}
Ниже мое Menu_pageFragment.java (нет ошибок):
package com.technology.computer.mit.ctechmit;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Menu_pageFragment extends Fragment {
public Menu_pageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu_page, container, false);
}
}
Вот мой home_page.java: (фрагмент-контейнер активности) (имеет ошибок)
package com.technology.computer.mit.ctechmit;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class Home_page extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
// Create a new Fragment to be placed in the activity layout
Home_pageFragment firstFragment = new Home_pageFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_home_page, menu);
return super.onCreateOptionsMenu(menu);
}
public void openSearch() {
}
public void openSettings() {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Может кто-нибудь, пожалуйста, помогите мне с этим?
Если я заменил getFragmentManager() на getSupportFragmentManager(), то он показывает мне сообщение об ошибке «Несовместимые типы. Обязательный параметр: android.app.FragmentTransaction Найдено: android.support .v4.app.FragmentTransaction –
Я отредактировал свой ответ, чтобы включить обновление, которое нужно сделать для FragmentTransaction. – ucsunil