如何调用从扩展活动的类扩展片段的类,我尝试查看各种教程和堆栈问题但是当我尝试实现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;
}
我的帐户详细信息片段。
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>
答案 0 :(得分:0)
问题在于您正在使用的Fragment类型。在活动中,您导入了标准FragmentManager,它处理标准碎片,而您的AccountDetailsFragment是v4碎片类型。
答案 1 :(得分:0)
确保MainAcivity
从android.support.v4.app.FragmentActivity
或android.support.v7.app.ActionBarActivity
延长,AccountDetailsFragment
从android.support.v4.app.Fragment
延长,FragmentManager
是getSupportFragmentManager()
的结果{{1}}