小白搞了一天fragment未能动态加载

walkuere 2019-06-20 02:45:47
静态加载发现照抄的可以了
fragment: https://www.cnblogs.com/linfenghp/p/5560015.html

AS 3.3.1
gradle
android.enableJetifier=true
android.useAndroidX=true

compileSdkVersion 28
defaultConfig {
applicationId "com.example.cd2"
minSdkVersion 22
targetSdkVersion 28

动态加载

API28 用androidX
MainActivity.java:


import Fragments.FW_EntryLobby;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


public class MainActivity extends AppCompatActivity {
public static MainActivity mainstance;
ViewGroup vg_mainframe;
FragmentActivity fa;//replaces FragmentManager
FragmentManager fm;
FragmentTransaction ft;//FragmentTransaction事务对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mainstance=this;
init_fragments();
loadWhole();
init_entrylobby();

}

private void init_fragments()//fragment is deprecated since API28
{
//https://stackoverflow.com/questions/51737667/since-the-android-getfragmentmanager-api-is-deprecated-is-there-any-alternati
fa=new FragmentActivity();
fm = fa.getSupportFragmentManager();
ft = fm.beginTransaction();
}
public void loadWhole()
{
vg_mainframe= (ViewGroup) MainActivity.mainstance.findViewById(R.id.fl_am);

}
FW_EntryLobby flayout_entrylobby;
public void init_entrylobby()
{
if(flayout_entrylobby==null)
flayout_entrylobby = new FW_EntryLobby();
ft.replace(R.id.fl_am,flayout_entrylobby);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();//用commit()就报错啊
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_am">
</FrameLayout>

</RelativeLayout>




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout class="Fragments.FW_EntryLobby"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
>

<Button
android:id="@+id/b_entrylobby_fetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"

android:text="enter" />

</RelativeLayout>



Fragment类


package Fragments;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.cd2.R;

public class FW_EntryLobby extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

View rootView = inflater.inflate(R.layout.lf_entrylobby, null);
return rootView;
}


}




开始运行就一片白,什么也没有
如果用commit()就是闪退
报错信息
Unable to start activity ComponentInfo{com.example.cd2/com.example.cd2.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed
...全文
1281 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
王能 2019-06-24
  • 打赏
  • 举报
回复
Activity的基础都没学啊,Activity被new是没有任何意义的 你把fa都换成this在试试
walkuere 2019-06-20
  • 打赏
  • 举报
回复
引用 2 楼 m-oj 的回复:
你为什么要new一个FragmentActivity呢,直接获取当前Activity的SupportFragmentManager就好了


我大概抄的时候,v4和androidx冲突,看到红线就自己写一个new,大概就是这个错误
m-oj 应用层 2019-06-20
  • 打赏
  • 举报
回复
你为什么要new一个FragmentActivity呢,直接获取当前Activity的SupportFragmentManager就好了
walkuere 2019-06-20
  • 打赏
  • 举报
回复
我用了他的就好了

https://blog.csdn.net/qq_34793358/article/details/83512177

也不知道错误在哪里

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧