错误的第二个参数类型找到'com.technology.computer.mit.ctechmit.Menu_pageFragment'必需'Android.app.Fragment'

时间:2015-07-14 15:35:04

标签: java android android-fragments

我已经编写了一个代码,用于点击发送按钮替换片段。但我的代码显示错误

Wrong 2nd argument type found 'com.technology.computer.mit.ctechmit.Menu_pageFragment' required 'Android.app.Fragment'

在“newfragment”一词的下面一行

transaction.replace(R.id.fragment_container, newFragment);

当我在网上搜索解决方案时,它说要扩展FragmentActivity而不是扩展Fragment。但是,如果我这样做,那么我的home_activity(一个没有错误)会显示许多错误。有人可以建议我解决这个问题吗?

下面是我的Home_pageFragment,其中sendmessage方法在其布局中单击send方法时将其替换为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);
    }

}

下面是我的home_page片段容器代码(没有错误):

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

    }
}

这是我的Menu_pageFragment:

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


}

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

看起来你正在尝试从另一个片段更改片段...... 尝试使用getChildFragmentManager()。

答案 1 :(得分:0)

将以下句子添加到您的fragement.java文件中:

import android.app.Fragment;