如何从片段中打开片段? [英] How to open a fragment from a fragment?

查看:51
本文介绍了如何从片段中打开片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个项目,其中主活动打开了片段(web.java),并且在单击时它在(web.java)中添加了一个按钮,它应该打开一个新的片段(next.java),但是当我运行应用程序时并点击按钮应用关闭.

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.

主要活动类别

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类

    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();


                                       }
                                   }

        );
    }
}

下一堂课

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;


}}

日志

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

推荐答案

在片段中,您应该在onCreateView内使用视图,而不是像在Activity上那样在onCreate内使用视图.因此,在Web片段中,您要做的是使用onCreate()中的按钮视图.这就是问题所在.

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();


                                   }
                               }

    );

因此,请尝试在onCreateView()中移动这部分代码.希望这行得通.

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

在下一个片段中,您将使用与Web片段相同的布局.

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

将R.layout.web_fragment(突出显示为红色)替换为下一个片段的不同布局文件.

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

这篇关于如何从片段中打开片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆