1

单击按钮后,我的应用程序中的一个活动会创建第二个活动。启动第二个活动的代码是这样的:

Button addAlarmButton = findViewById(R.id.addAlarmButton);
addAlarmButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getApplicationContext(), Activity_AddAlarm.class);
        startActivity(intent);
    }
});

该应用程序目前不完整。第二个活动有一个片段持有者,我以编程方式放置片段。(我将编写代码以添加更多片段。)

public class Activity_AddAlarm extends AppCompatActivity {

    private static FragmentManager fragmentManager;
    private static ActionBar actionBar;

    private static final String BACK_STACK_TAG = "addAlarmActivity_fragment_stack";

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

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar2));

        actionBar = getSupportActionBar();
        assert actionBar != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        fragmentManager = getSupportFragmentManager();

        fragmentManager.beginTransaction()
                .replace(R.id.addAlarmActFragHolder, new Fragment_AddAlarmActivity_AddAlarm())
                .addToBackStack(BACK_STACK_TAG)
                .commit();
        fragmentManager.executePendingTransactions();


    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    private void goBack() {
        if (fragmentManager.getBackStackEntryCount() > 1) {
            fragmentManager.popBackStackImmediate();
        } else {
            NavUtils.navigateUpFromSameTask(this);
        }
    }
}

片段类如下所示:

public class Fragment_AddAlarmActivity_AddAlarm extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_add_alarm_act_add_alarm, container, false);
    }
}

一旦我单击第一个活动中的按钮,onCreateView(...)片段类中的方法就会引发异常:

2020-07-19 01:15:13.315 9853-9853/in.basulabs.shakealarmclock E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException
    java.lang.NoSuchMethodException: android.graphics.FontFamily.<init> []
        at java.lang.Class.getConstructor0(Class.java:2332)
        at java.lang.Class.getConstructor(Class.java:1728)
        at androidx.core.graphics.TypefaceCompatApi26Impl.obtainFontFamilyCtor(TypefaceCompatApi26Impl.java:321)
        at androidx.core.graphics.TypefaceCompatApi26Impl.<init>(TypefaceCompatApi26Impl.java:84)
        at androidx.core.graphics.TypefaceCompatApi28Impl.<init>(TypefaceCompatApi28Impl.java:36)
        at androidx.core.graphics.TypefaceCompat.<clinit>(TypefaceCompat.java:47)
        at androidx.core.graphics.TypefaceCompat.create(TypefaceCompat.java:190)
        at androidx.appcompat.widget.AppCompatTextView.setTypeface(AppCompatTextView.java:705)
        at android.widget.TextView.resolveStyleAndSetTypeface(TextView.java:2183)
        at android.widget.TextView.setTypefaceFromAttrs(TextView.java:2154)
        at android.widget.TextView.applyTextAppearance(TextView.java:4105)
        at android.widget.TextView.<init>(TextView.java:1630)
        at android.widget.TextView.<init>(TextView.java:990)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:99)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95)
        at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
        at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:240)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1261)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1117)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at in.basulabs.shakealarmclock.Fragment_AddAlarmActivity_AddAlarm.onCreateView(Fragment_AddAlarmActivity_AddAlarm.java:17)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8008)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3382)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
2020-07-19 01:15:13.315 9853-9853/in.basulabs.shakealarmclock E/TypefaceCompatApi26Impl:     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

请注意,片段已正确加载,当我按下后退/向上按钮时,我可以毫无问题地返回上一个活动。

我更改了片段布局文件,以使用<include layout="@layout/..."/>标签将其包含在第二个活动的片段持有者中,而不是以编程方式添加片段。现在我在第二个活动中得到了一个异常setContentView(...),堆栈跟踪与上面完全相同,唯一的变化是现在它显示了onCreate(...)活动中的方法而不是onCreateView(...)片段中的方法。

我在运行 Google APIs x86 Intel Atom 系统映像的模拟器上运行此应用程序。SDK 工具版本为 30.0.1。

请注意,如果我在 API 25 模拟器上运行应用程序,我不会收到此错误。

这是 Android 11 平台中的错误吗?

我可以根据要求提供片段布局文件;我现在不包括它,因为它太大了。

模块级别的依赖关系build.gradle如下:

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
4

1 回答 1

2

专注于你的这一行:“java.lang.NoSuchMethodException: android.graphics.FontFamily”

将此核心库添加到您的应用程序 gradle 也可以解决您的问题(它解决了我的问题):

implementation 'androidx.core:core:1.3.1'

您可能需要查看此页面以获取更多信息。或者只记得在页面上将值设置为“当前稳定版本”。当您阅读本文时,它可能是 1.3.2 或更高版本。

于 2020-08-13T19:40:20.600 回答