繁体   English   中英

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

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

我如何从扩展活动的类中调用扩展片段的类,我尝试浏览各种教程并堆栈问题,但是当我尝试实现FragmentManager ...等时。 我在fragmentTransaction.add(R.id.LinearLayout1,fragment)行上收到错误; 声明“无法解析方法add(int,com.example.angrywords.AccountDetailsFragment)”

我是android新手,所以对您的帮助将不胜感激。 谢谢。

我的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;
    }

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

    }

还有我的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>

问题与您使用的片段类型有关。 在活动中,您导入了处理标准片段的标准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