如何扩展navHostFragment



我想要navHostFragment的扩展。但我不能!

首先,我创建了新的片段(CustomNavFragment(。它是扩展navHostFragment

public abstract class CustomNavFragment extends NavHostFragment {
public CustomNavFragment(){}
public abstract boolean customAction();
}

<CustomNavFragment.java>

public class LoginFragment extends CustomNavFragment {
public LoginFragment(){}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_login, container, false);
}
public boolean customAction() {
return false;
}
}

<登录碎片.java>

我创建了登录、加入、结果等片段。他们实施了自定义Action。当发生某些事件时,MainActivity调用当前片段的CustomAction

我的主要活动是下一个

public class MainActivity extends AppCompatActivity implements SomeEventListener{
NavController navController = null;
CustomNavFragment currentFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentFragment = (CustomNavFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
navController = currentFragment.getNavController();
}
@Override
public void eventListener(){
if(currentFragment.customAction()) // do something;
}

<MainActivity.java>

这是主要活动的xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="com.alticast.framework.ui.CustomNavFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>

<MainActivity.xml>

但当我运行时,它会显示下一个

(抛出MainActivity.java-onCreate((-setContentView(R.layout.activity_main((

Caused by: android.view.InflateException: Binary XML file line #9 in com.example.mypkg:layout/activity_main: Binary XML file line #9 in com.example.mypkg:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.mypkg:layout/activity_main: Error inflating class fragment
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.mypkg.ui.CustomNavFragment: make sure class name exists, is public, and has an empty constructor that is public
at androidx.fragment.app.Fragment.instantiate(Fragment.java:555)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:97)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.mypkg.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)

它说";确保类名存在,是公共的,并且有一个公共的空构造函数"但是他们有空的构造函数

谁来帮这个。

谢谢!

此行错误:

android:name="com.alticast.framework.ui.CustomNavFragment"

无法创建抽象类的实例。您应该将其替换为LoginFragmentCustomNavFragment的其他实现。

相关内容

  • 没有找到相关文章

最新更新