gpt4 book ai didi

java - 如何从 fragment 中打开 fragment ?

转载 作者:行者123 更新时间:2023-12-01 09:35:56 25 4
gpt4 key购买 nike

我制作了一个项目,其中主 Activity 打开 fragment (web.java),并在(web.java)中添加了一个按钮,当单击它时,它应该打开一个新 fragment (next.java),但是当我运行应用程序时然后单击按钮应用程序关闭。

主要 Activity 类别

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

最佳答案

只需在主布局的底部添加 LinearLayout 即可,如下所示,

之后只需添加这样的 fragment ,

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

关于java - 如何从 fragment 中打开 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931907/

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