简体   繁体   English

如何从活动中调用片段活动?

[英]How to call a fragment activity from an activity?

I am actually using in Android Studio for my Android application. 我实际上是在Android Studio中使用我的Android应用程序。 I have imported a module in my project. 我已经在项目中导入了一个模块。 The module consist of fragment activities. 该模块由片段活动组成。 I have compiled the module into the gradle app file and also included the module in the gradle settings file. 我已经将该模块编译到gradle应用程序文件中,并且还将模块包含在gradle设置文件中。 I want to call the fragment activity with a button from an activity, but when I am clicking on the button the application crashes. 我想使用活动中的按钮来调用fragment活动,但是当我单击按钮时,应用程序崩溃。 I have declared the fragment activity in my AndroidManifest.xml file but the application keeps on crashing. 我在AndroidManifest.xml文件中声明了片段活动,但应用程序不断崩溃。

Error when the application is crashing: 应用程序崩溃时发生错误:

在此处输入图片说明 I am using the following code to call the fragment activity : 我正在使用以下代码来调用fragment活动:

Button btn1 = (Button)findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
starActivity(new Intent(projectActivity.this, moduleFragmentActiviy.class));
}
});

I assume that I am getting this error because I am calling a fragment activity from an activity directly. 我假设收到此错误是因为我直接从一个活动中调用一个片段活动。 If this is the case, may I know how to call the fragment activity please. 如果是这种情况,请问我该如何调用片段活动。

If you have access to edit that module source code, that the right way to do this is to add intent-filter to FragmentActivity. 如果您有权编辑该模块源代码,那么执行此操作的正确方法是将Intent-filter添加到FragmentActivity。

In module's AndroidManifest.xml 在模块的AndroidManifest.xml中

<activity
    android:name=".FragmentAcitivity" >  
    <intent-filter>
        <action android:name="com.example.YOUR_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

And in MainActivity of main module: 并在主模块的MainActivity中:

Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent("com.example.YOUR_ACTION"));
    }
});

... and I hope you use UpperCamelCase for class naming ...并且我希望您使用UpperCamelCase进行类命名

Can you share your manifest file? 您可以共享清单文件吗? The syntax looks correct. 语法看起来正确。 As said in the error message - please check that activity class is declared, and, specifically, it is declared in manifest file . 如错误消息中所述-请检查是否声明了活动类,尤其是在清单文件中声明了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM