我试图从活动中启动片段(Listview)。 单击按钮时,开关应该开始。
My 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);
意图尝试的错误:
无法找到明确的活动类
我知道我不必将片段添加到AndroidManifest
感谢所有建议
编辑:将推出片段来源
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);
}
}
答案 0 :(得分:1)
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnAusgabenInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
此代码期望托管片段的活动实现OnAusgabenInteractionListener
。然后抛出一个不正确的异常,让你更进一步迷惑。
如果您想使用该代码,您的活动将需要实施OnAusgabenInteractionListener
,无论是什么。