简体   繁体   中英

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. I get a error on the line fragmentTransaction.add(R.id.LinearLayout1, fragment); Stating 'Cannot resolve method add(int, com.example.angrywords.AccountDetailsFragment)'

I'm fairly new to android so any help would be much appreciated. Thank you.

My 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

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)

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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