我创建了一个项目,其中主Activity打开片段(web.java),我在(web.java)中添加了一个按钮,当它点击它时应该打开一个新片段(next.java),但是当我运行app时然后点击关闭按钮app。
主要活动课程
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();
}
}
);
}
}
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
答案 0 :(得分:0)
在片段中,您应该使用onCreateView中的视图而不是onCreate中的视图,就像在Activity上一样。因此,在Web片段中,您所做的是使用onCreate()中的按钮视图。这导致了这个问题。
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()中移动这部分代码。希望这有效。
将R.layout.web_fragment(突出显示为红色)替换为Next片段的不同布局文件。
答案 1 :(得分:-1)
只需在内部主要布局的底部添加LinearLayout
,就像这样,
之后只需添加像这样的片段,
android.app.FragmentManager fn = getFragmentManager();
fn.beginTransaction().add(R.id.content_frame, new web()).addToBackStack("tag").commit();