我正在使用選項卡式活動。我在第一個標籤中使用了一個按鈕,在按鈕上點擊它應該到第二個標籤。如何實現意圖從另一個片段調用片段
如何使用意圖從第一片段調用第二片段?
這是我的兩個片段的代碼。
第一塊碎片:
package com.touchsmart.spectacular.ui.Fragments;
import....
public class ShippingFragment extends Fragment {
private LinearLayout addnew;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
View view = inflater.inflate(R.layout.fragment_shipping, container, false);
addnew = (LinearLayout) view.findViewById(R.id.addnew);
addnew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),PaymentFragment.class);
startActivity(i);
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Check Out");
}
}
第二塊碎片:
package com.touchsmart.spectacular.ui.Fragments;
import...
public class PaymentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
View view = inflater.inflate(R.layout.fragment_payment, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Check Out");
}
}
,我現在使用的代碼顯示在點擊第一個按鈕以下錯誤:
FATAL EXCEPTION: main Process: com.touchsmart.spectacular, PID: 18942
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.touchsmart.spectacular/com.touchsmart.spectacular.ui.Fragments.PaymentFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
at android.support.v4.app.Fragment.startActivity(Fragment.java:944)
at android.support.v4.app.Fragment.startActivity(Fragment.java:933)
at com.touchsmart.spectacular.ui.Fragments.ShippingFragment$1.onClick(ShippingFragment.java:36)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
人提供我通過這個解決方案。謝謝..
'PaymentFragment'是一個'Fragment',你應該用'FragmentManager'用'ShippingFragment'代替它 –
轉到這個鏈接[https://stackoverflow.com/a/25945639/7226732] – Mohammad