How to open a fragment from a fragment?

这一生的挚爱 提交于 2020-01-07 03:41:12

问题


I have made a project in which main Activity opens fragment(web.java) and I added a button in (web.java) when it clicked it should open a new fragment(next.java), but when I run app and click on button app close.

MAIN ACTIVITY class

public class MainActivity extends AppCompatActivity {

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

    setContentView(R.layout.activity_main);
    Button button1 = (Button) findViewById(R.id.button);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.app.FragmentManager fn = getFragmentManager();
            fn.beginTransaction().replace(R.id.content_frame, new web()).addToBackStack( "tag" ).commit();


        }
    }

    );

}

}

WEB class

    public class web extends Fragment {

    View v;

     @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.web_fragment, container, false);

        WebView mWebView = (WebView) v.findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String pdf_url = "http://mycbseguide.com/";
        mWebView.loadUrl(pdf_url);
        mWebView.setWebViewClient(new WebViewClient());


        return v;


    }


     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button2 = (Button) v.findViewById(R.id.next);
        button2.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           android.app.FragmentManager fn = getFragmentManager();
                                           fn.beginTransaction().replace(R.id.container, new next()).addToBackStack( "tag" ).commit();


                                       }
                                   }

        );
    }
}

NEXT class

public class next extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.web_fragment, container, false);

    WebView mWebView = (WebView) v.findViewById(R.id.webView);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    String pdf_url = "http://mycbseguide.com/";
    mWebView.loadUrl(pdf_url);
    mWebView.setWebViewClient(new WebViewClient());


    return v;


}}

LOG

08-13 17:33:49.943 30487-30487/com.example.prashant.webview E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.prashant.webview.next.access$super
08-13 17:33:49.944 30487-30487/com.example.prashant.webview E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.prashant.webview.next.access$super
08-13 17:33:49.953 30487-30487/com.example.prashant.webview E/FragmentManager: No view found for id 0x7f0b0064 (com.example.prashant.webview:id/container) for fragment next{42579268 #0 id=0x7f0b0064}
08-13 17:33:49.954 30487-30487/com.example.prashant.webview E/FragmentManager: Activity state:
08-13 17:33:49.955 30487-30487/com.example.prashant.webview E/FragmentManager:   Local FragmentActivity 4232d178 State:
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     mCreated=truemResumed=true mStopped=false mReallyStopped=false
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     mLoadersStarted=true
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:   FragmentManager misc state:
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     mHost=android.support.v4.app.FragmentActivity$HostCallbacks@4232e088
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@4232e088
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     mCurState=5 mStateSaved=false mDestroyed=false
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:   View Hierarchy:
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:     com.android.internal.policy.impl.PhoneWindow$DecorView{42346278 V.E..... ... 0,0-720,1184}
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:       android.widget.LinearLayout{42347638 V.E..... ... 0,0-720,1184}
08-13 17:33:49.956 30487-30487/com.example.prashant.webview E/FragmentManager:         android.view.ViewStub{4234e8c8 G.E..... ... 0,0-0,0 #1020312}
08-13 17:33:49.957 30487-30487/com.example.prashant.webview E/FragmentManager:         android.widget.FrameLayout{4234ecf8 V.E..... ... 0,50-720,1184}
08-13 17:33:49.957 30487-30487/com.example.prashant.webview E/FragmentManager:           android.support.v7.widget.ActionBarOverlayLayout{42353468 V.ED.... ... 0,0-720,1134 #7f0b0043 app:id/decor_content_parent}
08-13 17:33:49.957 30487-30487/com.example.prashant.webview E/FragmentManager:             android.support.v7.widget.ContentFrameLayout{423566b8 V.ED.... ... 0,112-720,1134 #1020002 android:id/content}
08-13 17:33:49.957 30487-30487/com.example.prashant.webview E/FragmentManager:               
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager: android.widget.RelativeLayout{4236d1a8 V.E..... ... 0,0-720,1022 #7f0b0054 app:id/content_frame}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:                 android.support.v7.widget.AppCompatButton{4236dd78 VFED..C. ... 272,463-448,559 #7f0b0055 app:id/button}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:             android.support.v7.widget.ActionBarContainer{42358c68 V.ED.... ... 0,0-720,112 #7f0b0044 app:id/action_bar_container}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:               android.support.v7.widget.Toolbar{423593e0 V.E..... ... 0,0-720,112 #7f0b0045 app:id/action_bar}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:                 android.widget.TextView{4236a558 V.ED.... ... 32,29-193,83}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:                 android.support.v7.widget.ActionMenuView{42384790 V.E..... ... 720,0-720,112}
08-13 17:33:49.959 30487-30487/com.example.prashant.webview E/FragmentManager:               android.support.v7.widget.ActionBarContextView{42363178 G.E..... ... 0,0-0,0 #7f0b0046 app:id/action_context_bar}
08-13 17:33:49.963 30487-30487/com.example.prashant.webview E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.prashant.webview, PID: 30487
                                                                              java.lang.IllegalArgumentException: No view found for id 0x7f0b0064 (com.example.prashant.webview:id/container) for fragment next{42579268 #0 id=0x7f0b0064}
`                                                                                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)

`

                                                                                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
                                                                                  at android.app.BackStackRecord.run(BackStackRecord.java:698)
                                                                                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
                                                                                  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
                                                                                  at android.os.Handler.handleCallback(Handler.java:808)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                  at android.os.Looper.loop(Looper.java:193)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5345)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
08-13 17:33:49.943 30487-30487/com.example.prashant.webview E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.prashant.webview.next.access$super

回答1:


Inside a fragment, you should use views inside onCreateView and not inside onCreate as you'd do on an Activity. So, in the web fragment, what you've done is used the button view inside onCreate(). This is causing the problem.

Button button2 = (Button) v.findViewById(R.id.next);
    button2.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       android.app.FragmentManager fn = getFragmentManager();
                                       fn.beginTransaction().replace(R.id.container, new next()).addToBackStack( "tag" ).commit();


                                   }
                               }

    );

So try moving this part of code inside your onCreateView(). Hope this works.

In the Next fragment, you are using the same layout as of the web fragment.

Replace the R.layout.web_fragment (highlighted red) to a different layout file for the Next fragment.




回答2:


Just add LinearLayout at bottom of inside main layout like this,

After that just add the fragment like this,

android.app.FragmentManager fn = getFragmentManager();
fn.beginTransaction().add(R.id.content_frame, new web()).addToBackStack("tag").commit();


来源:https://stackoverflow.com/questions/38931907/how-to-open-a-fragment-from-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!