gpt4 book ai didi

java - Android - 从 Activity 启动 fragment

转载 作者:行者123 更新时间:2023-11-30 02:29:41 24 4
gpt4 key购买 nike

我正在尝试从 Activity 中启动 fragment ( ListView )。单击按钮时,开关应启动。

我的 Activity 扩展自 Activity,Fragment 扩展自 ListFragment。

我试过了

FragmentManager fragmentManager  = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
AusgabenFragment ausgabenFragment = new AusgabenFragment();
transaction.replace(R.id.contentFragment, ausgabenFragment);
transaction.commit();

Intent intent;
intent = new Intent(getApplicationContext(), AusgabenFragment.class);
startActivity(intent);

Intent 尝试的错误:

Unable to find explicit activity class

我知道我不必将 fragment 添加到 AndroidManifest

谢谢大家的建议

编辑:将启动 fragment 源

public class AusgabenFragment extends ListFragment {

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnAusgabenInteractionListener mListener;

// TODO: Rename and change types of parameters
public static AusgabenFragment newInstance(String param1, String param2) {
AusgabenFragment fragment = new AusgabenFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public AusgabenFragment() {
}

private AusgabenDataSource dataSource;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

dataSource = new AusgabenDataSource(getActivity()); // context mit getActivity
try {
dataSource.open(); // kann Exception werfen daher try catch
} catch (SQLException e) {
e.printStackTrace();
}
List<Ausgaben> AusgabenList = dataSource.getAllAusgaben();
setListAdapter(new ArrayAdapter<Ausgaben>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, AusgabenList));
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnAusgabenInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}


@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Intent intent = new Intent(getActivity(),AusgabenDetailsActivity.class);
String Titel = l.getItemAtPosition(position).toString();
intent.putExtra("value_title",Titel);
getActivity().startActivity(intent);


if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.

}
}

public interface OnAusgabenInteractionListener {
// TODO: Update argument type and name
public void onAusgabenInteraction(String id);
}

最佳答案

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnAusgabenInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

此代码需要托管 fragment 的 Activity 来实现 OnAusgabenInteractionListener。然后它抛出一个不正确的异常,进一步混淆你。

如果您想使用该代码,您的 Activity 将需要实现 OnAusgabenInteractionListener,无论它是什么。

关于java - Android - 从 Activity 启动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com