Android Studio Fragment RoomListFragment 未附加到 Activity(在导航抽屉中开始新的 activity)
Android Studio Fragment RoomListFragment not attached to Activity (start new activity in navigation drawer)
我无法从 class RoomListFragment 打开新的 activity。我使用导航抽屉。
这是我的RoomListFragment.class
public class RoomListFragment extends Fragment {
private View root;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_rooms, container, false);
ListView listView = root.findViewById(R.id.listView);
RoomListAdapter adapter = new RoomListAdapter(root.getContext(),mTitle,mDescription);
listView.setAdapter(adapter);
return root;
}
void startRoomEditManagerActivity(Context context) {
Intent intent = new Intent(context, RoomEditManager.class);
startActivity(intent);
}
}
和我的第二个 class,我有一个按钮和一个激活功能 startRoomEditManagerActivity
RoomListAdapter(Context roomListContext, String[] roomListName, String[] roomListDescription) {
super(roomListContext, R.layout.row, R.id.testTextView1, roomListName);
this.roomListName = roomListName;
this.roomListDescription = roomListDescription;
this.contextRoomList = roomListContext;
}
@Override
public void onClick(View v) {
editRoomButton.setText(roomListName[position]);
Toast.makeText(getContext(), roomListName[position] +" id : " + String.valueOf(editRoomButton.getId()),Toast.LENGTH_SHORT).show();
RoomListFragment roomlistFragment = new RoomListFragment();
roomlistFragment.startRoomEditManagerActivity(contextRoomList);
}
感谢帮助^^
用
替换 onclick
@Override
public void onClick(View v) {
editRoomButton.setText(roomListName[position]);
Toast.makeText(getContext(), roomListName[position] +" id : " + String.valueOf(editRoomButton.getId()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(contextRoomList , RoomEditManager.class);
contextRoomList.startActivity(intent);
}
那么,在你的房间listFragment中,你不需要下面的代码
void startRoomEditManagerActivity(Context context) {
Intent intent = new Intent(context, RoomEditManager.class);
contextRoomList.startActivity(intent);
}
我无法从 class RoomListFragment 打开新的 activity。我使用导航抽屉。
这是我的RoomListFragment.class
public class RoomListFragment extends Fragment {
private View root;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_rooms, container, false);
ListView listView = root.findViewById(R.id.listView);
RoomListAdapter adapter = new RoomListAdapter(root.getContext(),mTitle,mDescription);
listView.setAdapter(adapter);
return root;
}
void startRoomEditManagerActivity(Context context) {
Intent intent = new Intent(context, RoomEditManager.class);
startActivity(intent);
}
}
和我的第二个 class,我有一个按钮和一个激活功能 startRoomEditManagerActivity
RoomListAdapter(Context roomListContext, String[] roomListName, String[] roomListDescription) {
super(roomListContext, R.layout.row, R.id.testTextView1, roomListName);
this.roomListName = roomListName;
this.roomListDescription = roomListDescription;
this.contextRoomList = roomListContext;
}
@Override
public void onClick(View v) {
editRoomButton.setText(roomListName[position]);
Toast.makeText(getContext(), roomListName[position] +" id : " + String.valueOf(editRoomButton.getId()),Toast.LENGTH_SHORT).show();
RoomListFragment roomlistFragment = new RoomListFragment();
roomlistFragment.startRoomEditManagerActivity(contextRoomList);
}
感谢帮助^^
用
替换 onclick @Override
public void onClick(View v) {
editRoomButton.setText(roomListName[position]);
Toast.makeText(getContext(), roomListName[position] +" id : " + String.valueOf(editRoomButton.getId()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(contextRoomList , RoomEditManager.class);
contextRoomList.startActivity(intent);
}
那么,在你的房间listFragment中,你不需要下面的代码
void startRoomEditManagerActivity(Context context) {
Intent intent = new Intent(context, RoomEditManager.class);
contextRoomList.startActivity(intent);
}