替换容器中的片段时发生java错误
我是安卓应用开发新手,目前正在学习片段的概念。我创建了一个片段容器活动(Home_page.java),默认情况下,它会膨胀Home_page片段。Home_pageFragment布局由一个EditText和一个按钮组成。单击按钮后,主页片段应替换为活动同一容器中的菜单页面片段。我已经编写了一段代码,用新的片段替换以前的片段
除了Home_pageFragment中的下一行之外,所有内容都可以编译。爪哇
transaction.replace(R.id.fragment_container, newFragment);
这显示了“newfragment”一词下的一个错误:
Wrong 2nd argument type found 'com.technology.computer.mit.ctechmit.Menu_pageFragment' required 'Android.app.Fragment'
有人能给我一个解决方案吗
下面是我的主页。java,其中sendmessage()方法在其布局中单击send按钮时将其替换为Menu_pageFragment:
package com.technology.computer.mit.ctechmit;
import 安卓.app.FragmentTransaction;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.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);
}
}
下面是我的菜单。java(没有错误):
package com.technology.computer.mit.ctechmit;
import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.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);
}
}
这是我的主页。java:(片段容器活动)(没有错误)
package com.technology.computer.mit.ctechmit;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuInflater;
import 安卓.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);
}
}
}
有人能帮我吗
# 1 楼答案
替换以下行:
与:
您使用的是支持库中的片段,可以从导入中看到:
但是安卓希望看到一个常规片段的实例,这可以从抛出的异常中看出
所以有一个例外,因为它们是不兼容的类型
因此,您不应该使用getFragmentManager(),而应该使用getSupportFragmentManager()。您的另一个选项是从常规片段类(android.app.Fragment)继承,在这种情况下,您可以继续使用getSupportFragmentManager()
编辑:我刚刚注意到,要使用android,还需要更新FragmentTransaction。支持v4。应用程序。FragmentTransaction,如果要继续使用支持库中的片段
# 2 楼答案
替换
与
无法从支持库导入
Fragment
并使用本机FragmentManFger
Fragment
的getFragmentManager()返回支持片段管理器