gpt4 book ai didi

android - 在主要 Activity 中添加 fragment

转载 作者:行者123 更新时间:2023-11-29 14:24:07 26 4
gpt4 key购买 nike

我正在关注Android Udacity课程,但该课程已过时,因此我尝试通过向主要活动中手动添加一个片段来自我更新。
这段代码可以正常运行,但是当应用启动时崩溃,这使我在android显示器中出现了很长的错误。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(savedInstanceState == null)
{
getFragmentManager().beginTransaction().add(R.id.container,new PHFragment()).commit();
}
}
}




public class PHFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Inflate the layout for this fragment
return rootView;
}
}


XML格式
fragment_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.athomield.mysunshine.MainActivity"
android:touchscreenBlocksFocus="false">

<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</FrameLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.athomield.mysunshine.MainActivity"
android:touchscreenBlocksFocus="false">

</FrameLayout>


list_itemm_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_item_forecast_textview"
android:gravity="center_vertical"
>
</TextView>


日志

06-23 11:23:02.878 3095-3095 / com.example.athomield.mysunshine W /系统:ClassLoader引用的未知路径:/data/app/com.example.athomield.mysunshine-1/lib/x86
06-23 11:23:06.230 3095-3095 / com.example.athomield.mysunshine W /系统:ClassLoader引用的未知路径:/data/app/com.example.athomield.mysunshine-1/lib/x86
06-23 11:23:06.408 3095-3105 / com.example.athomield.mysunshine W / art:暂停所有线程占用:58.067ms
06-23 11:23:06.417 3095-3105 / com.example.athomield.mysunshine I / art:背景粘性并发标记清除GC已释放13182(1591KB)AllocSpace对象,9(208KB)LOS对象,73%可用空间,991KB / 3MB,已暂停64.383ms,总计75.476ms
06-23 11:23:06.509 3095-3095 / com.example.athomield.mysunshine W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter ,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)错误地覆盖了android.graphics.drawable.Drawable中的package-private方法
06-23 11:23:06.685 3095-3101 / com.example.athomield.mysunshine W / art:暂停所有线程占用:29.223ms
06-23 11:23:06.772 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:找不到片段PHFragment {ccbb2d0#0 id的ID 0x7f0b0054(com.example.athomield.mysunshine:id / container)的视图= 0x7f0b0054}
06-23 11:23:06.772 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:活动状态:
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:本地FragmentActivity b704817状态:
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:mCreated = falsemResumed = false mStopped = false mReallyStopped = false
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:mLoadersStarted = false
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:FragmentManager杂项状态:
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:mHost=android.support.v4.app.FragmentActivity$HostCallbacks@16b2c9
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@16b2c9
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:mCurState = 1 mStateSaved = false mDestroyed = false
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:视图层次结构:
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:com.android.internal.policy.PhoneWindow $ DecorView {4998dce VE .... ... 0,0-0, 0}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.widget.LinearLayout {9ea54ef V.E ..... ... 0,0-0,0}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.view.ViewStub {e812bfc GE .... ... 0,0-0,0#10203a9 android:id / action_mode_bar_stub}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.widget.FrameLayout {94​​eb285 V.E ..... ... 0,0-0,0}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.ActionBarOverlayLayout {8c4b8da VE .... ... 0,0-0,0# 7f0b0043 app:id / decor_content_parent}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.ContentFrameLayout {eb1510b VE .... ... 0,0-0,0# 1020002 android:id / content}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.widget.RelativeLayout {1b30be8 V.E ..... ... 0,0-0,0}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.AppCompatButton {44cf201 VFED..C。 ... 0,0-0,0#7f0b0055 app:id / button}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.AppCompatButton {44588a6 VFED..C。 ... 0,0-0,0#7f0b0056 app:id / button2}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.ActionBarContainer {7a1f2e7 V.ED .... ... 0,0-0, 0#7f0b0044 app:id / action_bar_container}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.Toolbar {da0fe94 VE .... ... 0,0-0,0# 7f0b0045 app:id / action_bar}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.widget.TextView {595ad3d V.ED .... ... 0,0-0,0}
06-23 11:23:06.773 3095-3095 / com.example.athomield.mysunshine E / FragmentManager:android.support.v7.widget.ActionBarContextView {5b18932 GE .... ... 0,0-0,0# 7f0b0046 app:id / action_context_bar}
06-23 11:23:06.774 3095-3095 / com.example.athomield.mysunshine D / AndroidRuntime:关闭VM

                                                                            --------- beginning of crash


06-23 11:23:06.774 3095-3095 / com.example.athomield.mysunshine E / AndroidRuntime:FATAL EXCEPTION:main
                                                                                流程:com.example.athomield.mysunshine,PID:3095
                                                                                java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.athomield.mysunshine / com.example.athomield.mysunshine.MainActivity}:java.lang.IllegalArgumentException:未找到ID为0x7f0b0054(com.example.athomield的视图)。片段PHFragment {mysunshine:id / container){ccbb2d0#0 id = 0x7f0b0054}
                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                    在android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    在android.os.Looper.loop(Looper.java:148)
                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    在java.lang.reflect.Method.invoke(本机方法)
                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                 造成原因:java.lang.IllegalArgumentException:找不到片段PHFragment {ccbb2d0#0 id = 0x7f0b0054}的ID 0x7f0b0054(com.example.athomield.mysunshine:id / container)的视图
                                                                                    在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:965)
                                                                                    在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                    在android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                    在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                                    在android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                                    在android.app.Activity.performStart(Activity.java:6252)
                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                    在android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    在android.os.Looper.loop(Looper.java:148)
                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    在java.lang.reflect.Method.invoke(本机方法)
                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

在这里,您可以像这样去:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
.....
private FragmentTransaction mFragmentTransaction;
private FragmentManager mFragmentManager;
.....

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.content_frame, new PhfFragment());
mFragmentTransaction.commit();

关于android - 在主要 Activity 中添加 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37981290/

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