当尝试加载来自另一个片段布局的片段时,我的应用程序停止了。
查看logcat时出现此错误:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.flow/com.example.flow.displayClasses.GroupScreens.HomeGroupFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:881)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:995)
at android.support.v4.app.Fragment.startActivity(Fragment.java:1084)
at android.support.v4.app.Fragment.startActivity(Fragment.java:1073)
at com.example.flow.displayClasses.GroupScreens.GroupsFragment$1.onItemClick(GroupsFragment.java:170)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
我来自哪里的片段:
package com.example.flow.displayClasses.GroupScreens;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import com.example.flow.R;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.flow.classes.Friends;
import com.example.flow.classes.Group;
import com.example.flow.classes.User;
import com.example.flow.displayClasses.FriendsScreen.FriendsFragment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.view.MenuItem;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class GroupsFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
ArrayList<Group> groups = new ArrayList<Group>();
public GroupsFragment() {
// Required empty public constructor
}
public static GroupsFragment newInstance(String param1, String param2) {
GroupsFragment fragment = new GroupsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_groups, container, false);
// Hardcoded for testing
User u1 = new User("Bram", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u2 = new User("Bram1", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u3 = new User("Bram2", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u4 = new User("Bram3", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u5 = new User("Bram4", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u6 = new User("Bram5", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u7 = new User("Bram6", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u8 = new User("Bram7", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u9 = new User("Bram8", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u10 = new User("Bram9", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u11 = new User("Bram10", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u12 = new User("Bram11", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u13 = new User("Bram12", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u14 = new User("Bram13", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u15 = new User("Bram14", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u16 = new User("Bram15", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u17 = new User("Bram16", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u18 = new User("Bram17", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
ArrayList<User> ug1 = new ArrayList<User>();
ug1.add(u1);
ug1.add(u2);
ug1.add(u3);
ArrayList<User> ug2 = new ArrayList<User>();
ug2.add(u4);
ug2.add(u5);
ug2.add(u6);
//Collections.addAll(ug1, u4, u5, u6);
ArrayList<User> ug3 = new ArrayList<User>();
ug3.add(u1);
ug3.add(u7);
ug3.add(u8);
ug3.add(u9);
//Collections.addAll(ug1, u7, u8, u9);
ArrayList<User> ug4 = new ArrayList<User>();
Collections.addAll(ug1, u10, u11, u12);
ArrayList<User> ug5 = new ArrayList<User>();
Collections.addAll(ug1, u13, u14, u15);
ArrayList<User> ug6 = new ArrayList<User>();
Collections.addAll(ug1, u14, u5, u6);
ArrayList<User> ug7 = new ArrayList<User>();
Collections.addAll(ug1, u4, u10, u6, u18, u17);
ArrayList<User> ug8 = new ArrayList<User>();
Collections.addAll(ug1, u4, u5, u6);
Group g1 = new Group(1, ug1, u1, "budies", u1, "date", true);
Group g8 = new Group(2, ug1, u1, "budies", u1, "date", false);
Group g9 = new Group(3, ug1, u1, "budies", u1, "date", true);
Group g10 = new Group(4, ug1, u1, "budies", u1, "date", false);
Group g11 = new Group(5, ug1, u1, "budies", u1, "date", true);
Group g12 = new Group(6, ug1, u1, "budies", u1, "date", false);
Group g13 = new Group(7, ug1, u1, "budies", u1, "date", false);
Group g14 = new Group(8, ug1, u1, "budies", u1, "date", false);
Group g15 = new Group(9, ug1, u1, "budies", u1, "date", true);
Group g16 = new Group(10, ug1, u1, "budies", u1, "date", true);
Group g17 = new Group(11, ug1, u1, "budies", u1, "date", true);
Group g18 = new Group(12, ug1, u1, "budies", u1, "date", false);
Collections.addAll(groups, g1, g8, g9, g10, g11, g12, g13, g14, g15, g16, g17, g18);
ListView list = (ListView)RootView.findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(getActivity().getApplicationContext(), HomeGroupFragment.class);
intent.putExtra("group", groups.get(position));
startActivity(intent);
}
});
return RootView;
}
class CustomAdapter extends BaseAdapter {
int index;
@Override
public int getCount() {
return groups.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int id) {
return id ;
}
public View getView(int i, View view, ViewGroup viewgroup) {
if (groups.get(i).isPrepaid()) {
view = getLayoutInflater().inflate(R.layout.groups_prepaid_layout, null);
} else {
view = getLayoutInflater().inflate(R.layout.groups_layout, null);
}
TextView textView = view.findViewById(R.id.Groupname);
ImageView image = view.findViewById(R.id.member);
String t = groups.get(i).getName();
textView.setText(t);
return view;
}
}
}
无法加载的片段:
package com.example.flow.displayClasses.GroupScreens;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import com.example.flow.Home;
import com.example.flow.R;
import com.example.flow.classes.Group;
import com.example.flow.classes.GroupExpense;
import com.example.flow.classes.UserExpense;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
public class HomeGroupFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
ArrayList<GroupExpense> expense = new ArrayList<GroupExpense>();
public HomeGroupFragment() {
// Required empty public constructor
}
public static HomeGroupFragment newInstance(String param1, String param2) {
HomeGroupFragment fragment = new HomeGroupFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
Group group;
boolean status;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_group, container, false);
/*
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_group);
ConstraintLayout layout =getActivity().findViewById(R.id.groupLayout);
status = true;*/
//harcoded
UserExpense ux = new UserExpense(group.getUsers().get(2), 5.3);
UserExpense ux1 = new UserExpense(group.getUsers().get(3), 6.91);
ArrayList<UserExpense> uxs = new ArrayList<UserExpense>();
Collections.addAll(uxs, ux, ux1);
GroupExpense ex = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex1 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex2 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex3 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex4 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex5 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex6 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex7 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex8 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex9 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex10 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex11 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex12 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex13 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex14 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
GroupExpense ex15 = new GroupExpense(1, group.getUsers().get(1), "pitta", "voor te eten", 12.21, "13 oktober 2018", uxs);
Collections.addAll(expense, ex, ex1, ex2, ex3, ex4, ex5, ex6, ex7, ex8, ex9, ex10, ex11, ex12, ex13, ex14, ex15);
Intent intent = getActivity().getIntent();
group = Objects.requireNonNull(intent.getExtras()).getParcelable("group");
int id = intent.getIntExtra("group", -1);
TextView t = getActivity().findViewById(R.id.Groupname);
t.setText(group.getName());
t.setId(group.getGroupId());
setLayout();
return RootView;
}
public void changeToMember(View view)
{
Button member =getActivity().findViewById(R.id.member);
Button history =getActivity().findViewById(R.id.history);
if (status) {
member.setBackgroundResource(R.drawable.bottom_border_selected);
member.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
member.setTypeface(null, Typeface.BOLD);
member.setTextColor(Color.parseColor("#4799E8"));
history.setBackgroundResource(R.drawable.bottom_border);
history.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
history.setTypeface(null, Typeface.NORMAL);
history.setTextColor(Color.parseColor("#000000"));
status = false;
setLayout();
}
}
public void changeToHistory(View view)
{
Button member = getActivity().findViewById(R.id.member);
Button history =getActivity().findViewById(R.id.history);
if (!status) {
member.setBackgroundResource(R.drawable.bottom_border);
member.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
member.setTypeface(null, Typeface.NORMAL);
member.setTextColor(Color.parseColor("#000000"));
history.setBackgroundResource(R.drawable.bottom_border_selected);
history.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
history.setTypeface(null, Typeface.BOLD);
history.setTextColor(Color.parseColor("#4799E8"));
status = true;
setLayout();
}
}
public void setLayout()
{
ListView list = getActivity().findViewById(R.id.expenseList);
CustomAdapter ad = new CustomAdapter();
list.setAdapter(ad);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (status) {
Intent intent = new Intent(getActivity().getApplicationContext(), GroupExpenseFragment.class);
intent.putExtra("expense", expense.get(position));
startActivity(intent);
} else {
Intent intent = new Intent(getActivity().getApplicationContext(), Home.class);
intent.putExtra("group", group.getUsers().get(position));
startActivity(intent);
}
}
});
}
public void leaveGroup(View view)
{
}
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount()
{
return status ? expense.size() : group.getUsers().size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int id)
{
return id;
}
public View getView(int i, View view, ViewGroup viewgroup)
{
if (status) {
view = getLayoutInflater().inflate(R.layout.group_expense_layout, null);
TextView user = view.findViewById(R.id.username);
TextView subject = view.findViewById(R.id.subject);
TextView amount = view.findViewById(R.id.expenseAmount);
user.setText(expense.get(i).getCreditor().getName());
subject.setText(expense.get(i).getName());
amount.setText(String.valueOf(expense.get(i).getAmount()));
} else {
view = getLayoutInflater().inflate(R.layout.group_member_layout, null);
TextView name = view.findViewById(R.id.nameMember);
TextView admin = view.findViewById(R.id.privilege);
TextView date = view.findViewById(R.id.date);
name.setText(group.getUsers().get(i).getName());
admin.setText("");
date.setText("");
}
return view;
}
}
}
fragment_group.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".displayClasses.GroupScreens.HomeGroupFragment">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/groupLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".displayClasses.GroupScreens.GroupFragment">
<TextView
android:id="@+id/Groupname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/group_name"
android:textColor="#428EC4"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/member"
style="@style/selected"
android:layout_width="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="@string/history"
android:textAllCaps="false"
app:layout_constraintEnd_toStartOf="@+id/history"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Groupname" />
<Button
android:id="@+id/history"
style="@style/unselected"
android:layout_width="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="@string/members"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/member"
app:layout_constraintTop_toBottomOf="@+id/Groupname" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/history">
<ListView
android:id="@+id/expenseList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/leave_group"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
group_expense_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="25dp"
android:text="Username"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/username"
android:layout_marginStart="65dp"
android:layout_toEndOf="@+id/username"
android:text="Name" />
<TextView
android:id="@+id/expenseAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/username"
android:layout_alignParentEnd="true"
android:layout_marginStart="93dp"
android:layout_marginEnd="25dp"
android:layout_toEndOf="@+id/subject"
android:text="TextView" />
</RelativeLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.flow">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label=" "
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:icon,android:label">
<activity
android:theme="@style/AppTheme"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="@style/AppTheme.NoActionBar"
android:name=".Home"
android:parentActivityName=".displayClasses.LoginScreens.Login">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".displayClasses.LoginScreens.Login" />
</activity>
<activity
android:theme="@style/AppTheme"
android:name=".displayClasses.LoginScreens.activity_signup" />
<activity
android:theme="@style/AppTheme"
android:name=".displayClasses.LoginScreens.Login" />
</application>
</manifest>
但是据我所知,我不应该在清单中实现片段。那我需要重写Intent语句吗?是的,我该如何正确执行?我正在学习使用Android Studio的任何帮助,不胜感激。
答案 0 :(得分:0)
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.flow/com.example.flow.displayClasses.GroupScreens.HomeGroupFragment}; have you declared this activity in your AndroidManifest.xml?
您是否在清单中声明了活动?
编辑:
这里的代码
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(getActivity().getApplicationContext(), HomeGroupFragment.class);
intent.putExtra("group", groups.get(position));
startActivity(intent);
}
这不是初始化片段的方式。 startActivity()用于启动活动。这就是崩溃发生的原因。
要使用香精,您必须执行碎片交易。在活动中创建一个FrameLayout,在其中添加该片段。 更多信息How do I add a Fragment to an Activity with a programmatically created content view
答案 1 :(得分:0)
您需要导入以下库:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
然后在onClick中,您需要调用片段转换进行显示 一个片段。目的是用于活动:
HomeGroupFragment f = HomeGroupFragment.newInstance(position); // call the fragment you want to display with new instance
//f.setArguments(bundle); // if you want to send arguments
FragmentManager fragmentManager = getSupportFragmentManager(); // for transaction, call fragment manager
fragmentManager.beginTransaction().replace(R.id.main_frame, f).addToBackStack(null).commit();
// addToBackStack = optional if ypu want to add the fragment to stack
// commit = end the transaction
如果要发送参数:
Bundle bundle = new Bundle();
bundle.putInt("position", groups.get(position));
bundle.putString("anyText", groups.getText());