替换容器中的片段时出错

时间:2015-07-15 17:28:32

标签: java android android-fragments android-fragmentactivity

我是Android app开发的新手,我目前正在学习片段的概念。我创建了一个片段容器活动(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);
        }

    }
}

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

替换以下行:

FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();

使用:

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

您使用的是来自支持库的片段,可以从导入中看到:

import android.support.v4.app.Fragment;

但Android正在期待一个常规Fragment的实例,可以从抛出的异常中看出。

  

需要'Android.app.Fragment'

所以有一个例外,因为它们是不兼容的类型。

因此,您应该使用getSupportFragmentManager()而不是使用getFragmentManager()。你的另一个选择是继承常规的Fragment类(android.app.fragment),在这种情况下你可以继续使用getSupportFragmentManager()

编辑:我刚刚注意到,如果要继续使用支持库中的片段,还需要更新FragmentTransaction以使用android.support.v4.app.FragmentTransaction。

答案 1 :(得分:0)

替换

FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();

FragmentTransaction transaction = getFragmentManager().beginTransaction();

您无法从支持库导入Fragment并使用原生FragmentManFger。您的案例中的Fragment的getFragmentManager()会返回支持片段管理器