有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调用从扩展活动的MainActivity类扩展片段的类片段

如何调用从扩展活动的类中扩展片段的类,我已经尝试查看了各种教程和堆栈问题,但是当我尝试实现FragmentManager时。。。等 我在fragmentTransaction行上遇到一个错误。添加(R.id.LinearLayout1,片段);陈述 '无法解析方法add(int,com.example.angrywords.AccountDetailsFragment)'

我对安卓系统还比较陌生,所以非常感谢您的帮助。 多谢各位

我的维护。阶级

import 安卓.app.Activity;
import 安卓.app.FragmentManager;
import 安卓.app.FragmentTransaction;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.v7.app.ActionBarDrawerToggle;
import 安卓.text.format.Time;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.view.Window;
import 安卓.widget.Button;
import 安卓.widget.Toast;

public class MainActivity extends Activity implements OnRetrieveHttpData, View.OnClickListener {
    ActionBarDrawerToggle mDrawerToggle;
    Button logoutbtn;
    Button Optionsbtn;
    private String mUserName;
    private String mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the ActionBar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Set Layout
        setContentView(R.layout.activity_main);

        SharedPreferencesWrapper.getFromPrefs(this, "username", mUserName);
        SharedPreferencesWrapper.getFromPrefs(this, "password", mPassword);

        logoutbtn = (Button) findViewById(R.id.logoutButton);
        logoutbtn.setOnClickListener(MainActivity.this);

        Optionsbtn = (Button) findViewById(R.id.optionsButton);

        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                AccountDetailsFragment fragment = new AccountDetailsFragment();
                fragmentTransaction.add(R.id.LinearLayout1, fragment);
                fragmentTransaction.commit();
            }
        };

        Optionsbtn.setOnClickListener(listener);
    }

    public void NewPuzzleButtonClicked(View v) {
        if (v.getId() == R.id.newPuzzleButton) {
            //handle the click here
            startNewPuzzleActivity();
        }
    }

    public void SavedPuzzleButtonClicked(View v) {
        if (v.getId() == R.id.savedPuzzlesButton) {
            //handle the click here
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

我的帐户详细信息片段。阶级

import 安卓.os.Bundle;
import 安卓.support.v4.app.DialogFragment;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.TextView;

public class AccountDetailsFragment extends Fragment {
    Button changePasswordbtn;
    Button unregisterbtn;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View v;
            v = inflater.inflate(R.layout.activity_account, container, false);
            changePasswordbtn = (Button)v.findViewById(R.id.changePasswordbtn);
            unregisterbtn = (Button)v.findViewById(R.id.unregisterbtn);

            TextView mName = (TextView) v.findViewById(R.id.account_details_name_textview);
            String mFirstName = SharedPreferencesWrapper.getFromPrefs(getActivity(), "first_name", "First Name");
            String mLastName = SharedPreferencesWrapper.getFromPrefs(getActivity(), "last_name", "Last Name");
            mName.setText(mFirstName + " " + mLastName);

            TextView mUsername = (TextView) v.findViewById(R.id.account_details_username_textview);
            mUsername.setText(SharedPreferencesWrapper.getFromPrefs(getActivity(), "username", "Username"));

            TextView mPassword = (TextView) v.findViewById(R.id.account_details_password_textview);
            mPassword.setText(SharedPreferencesWrapper.getFromPrefs(getActivity(), "password", "Password"));

            changePasswordbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment changePasswordFragment = new ChangePasswordDialogFragment();
                    changePasswordFragment.show(getFragmentManager(), "DIALOG_CHANGE_PASSWORD");
                }
            });
            unregisterbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment unRegisterFragment = new UnregisterDialogFragment();
                    unRegisterFragment.show(getFragmentManager(), "DIALOG_UNREGISTER");
                }
            });
         return v;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_main, menu);
            super.onCreateOptionsMenu(menu, inflater);
        }

    }

我的活动是主要的。xml(如果有帮助的话)

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/LinearLayout1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:baselineAligned="false"
安卓:gravity="bottom"
安卓:orientation="horizontal"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent"
    安卓:layout_marginTop="36dp"
    安卓:layout_weight="1"
    安卓:orientation="vertical"
    安卓:weightSum="1">

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="20dp"
        安卓:gravity="center|bottom" >

        <TextView
            安卓:id="@+id/welcomeTextView"
            安卓:layout_width="wrap_content"
            安卓:layout_height="143dp"
            安卓:shadowDx="1"
            安卓:shadowDy="1"
            安卓:shadowRadius="2"
            安卓:text="label main welcome"
            安卓:textAppearance="?安卓:attr/textAppearanceLarge"
            安卓:textSize="26sp"
            安卓:textStyle="bold"
            安卓:typeface="normal"
            安卓:layout_gravity="center_vertical" />

    </LinearLayout>

    <TextView
        安卓:id="@+id/currentUserTextView"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginLeft="8dp"
        安卓:text="label_main_sub"
        安卓:textAppearance="?安卓:attr/textAppearanceMedium"
        安卓:textSize="14sp"
        安卓:layout_gravity="center_horizontal"
        安卓:layout_weight="0.22" />

    <Button
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="Log out"
        安卓:id="@+id/logoutButton"
        安卓:layout_gravity="center_horizontal" />

</LinearLayout>

<LinearLayout
    安卓:layout_width="316dp"
    安卓:layout_height="fill_parent"
    安卓:gravity="center"
    安卓:orientation="vertical" >

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="8dp" >

        <Button
            安卓:id="@+id/newPuzzleButton"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:onClick="NewPuzzleButtonClicked"
            安卓:text="New Puzzle"/>

    </LinearLayout>

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="8dp" >

        <Button
            安卓:id="@+id/savedPuzzlesButton"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Resume Saved Puzzle" />

    </LinearLayout>

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" >

        <Button
            安卓:id="@+id/optionsButton"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Options" />

    </LinearLayout>
    <FrameLayout
        安卓:id="@+id/FragmentContainer"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"/>

</LinearLayout>


共 (2) 个答案

  1. # 1 楼答案

    问题在于您使用的片段类型。在活动中,您导入了标准FragmentManager,该管理器处理标准片段,而AccountDetailsFragment是v4 Fragment类型

  2. # 2 楼答案

    确保您的MainAcivityandroid.support.v4.app.FragmentActivityandroid.support.v7.app.ActionBarActivity扩展,并且您的AccountDetailsFragmentandroid.support.v4.app.Fragment扩展,并且您的FragmentManagergetSupportFragmentManager()的结果