简体   繁体   English

调用从MainActivity类扩展Activity的Fragment扩展的类Fragment

[英]Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity

How do I call a class which extends a fragment from a class which extends an activity, I have tried looking through various tutorials and stacks questions but when I try to implement the FragmentManager...etc. 我如何从扩展活动的类中调用扩展片段的类,我尝试浏览各种教程并堆栈问题,但是当我尝试实现FragmentManager ...等时。 I get a error on the line fragmentTransaction.add(R.id.LinearLayout1, fragment); 我在fragmentTransaction.add(R.id.LinearLayout1,fragment)行上收到错误; Stating 'Cannot resolve method add(int, com.example.angrywords.AccountDetailsFragment)' 声明“无法解析方法add(int,com.example.angrywords.AccountDetailsFragment)”

I'm fairly new to android so any help would be much appreciated. 我是android新手,所以对您的帮助将不胜感激。 Thank you. 谢谢。

My MainAcivity.class 我的MainAcivity.class

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.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;
    }

My AccountDetailsFragment.class 我的AccountDetailsFragment.class

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.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);
        }

    }

And my activity_main.xml (if it helps) 还有我的activity_main.xml(如果有帮助的话)

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

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

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

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

    </LinearLayout>

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

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

</LinearLayout>

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

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

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

    </LinearLayout>

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

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

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

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

</LinearLayout>

The issue is with the type of Fragment that you are using. 问题与您使用的片段类型有关。 In the activity you have imported the standard FragmentManager which deals with the standard Fragments whereas your AccountDetailsFragment is of type v4 Fragment. 在活动中,您导入了处理标准片段的标准FragmentManager,而AccountDetailsFragment的类型为v4 Fragment。

确保您的MainAcivity是从android.support.v4.app.FragmentActivityandroid.support.v7.app.ActionBarActivity扩展的,而AccountDetailsFragment是从android.support.v4.app.Fragment扩展的,而FragmentManagergetSupportFragmentManager()的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM