Java 多个活动的多个意图过滤器

Java 多个活动的多个意图过滤器,java,android,android-intent,android-activity,android-manifest,Java,Android,Android Intent,Android Activity,Android Manifest,我有两个与两个按钮相关的活动 <?xml version="1.0" encoding="utf-8"?> 布局代码 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:background="@drawable/main" android:layout_

我有两个与两个按钮相关的活动

<?xml version="1.0" encoding="utf-8"?>
布局代码

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/main"
    android:background="@drawable/main"
    android:layout_centerHorizontal="true"
    android:id="@+id/main_text_view"


    android:layout_marginTop="17dp"


    />

在这种情况下

子活动按钮打开子活动

但MainActivity按钮没有打开main

但如果我把主活动置于次活动之上

子活动按钮未打开子活动


但是MainActivity按钮打开了main

好的,根据您对我评论的回复,我想我能说的就是您需要遵循一个关于如何为android创建应用程序的教程。网上有很多优秀的教程

您有两个活动,因此应该有两个布局文件。你只发了一条

您还应该有两个源文件,每个活动一个。你只发了一条

布局中您希望在代码中引用的每个android元素都需要一个ID。您的代码引用
R.ID.main\u text\u view
,但布局文件中没有此类ID。我很惊讶你的代码居然能编译

但是,要回答您的具体问题,您需要以下内容:

1) 清单文件中的
标记必须与每个活动的java类源文件的名称匹配。因此,根据清单文件,您的活动类文件似乎被称为“MainActivity”和“Subactivity”。但是,请参见下面我对onClickListener代码的评论

此外,您的清单表明您的两个活动都是“启动器”活动。您只需要为您希望能够从Android应用程序启动器启动的活动(即手机上安装的所有应用程序的列表)添加该标签。似乎您只希望在主要活动中使用此选项,但如果愿意,可以指定多个选项

2) 您的活动是彼此的对偶(也就是说,它们听起来好像做了完全相同的事情——每个都有一个启动另一个的按钮),因此代码将非常相似。MainActivity的代码应如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    addListenerOnButton();
}

public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.main_text_view);  //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate().  However, the id you specify doesn't exist in your layout file.  This should either not compile or return null.

//This is fine.
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, main.class);  //<--"main.class" doesn't match either of the activity names declared in your manifest.  It should match one of the names declared in the <name> tag of one of your <activity> tags.
            startActivity(intent);

        }

    });

}
现在,您必须在子活动代码中执行相同的操作,但onClickListener将调用主活动而不是子活动。因此,您的MainActivity(启动子活动)的onClickListener代码如下所示:

在你的子活动中也是这样(启动你的主要活动)


张贴相关代码。清单文件不足以控制按钮的操作。你的按钮是你的应用程序的一部分,还是你在谈论android应用程序菜单中的启动器?按钮也是我的应用程序@rothlouppost布局文件的一部分,请。谢谢你的时间,但我需要告诉你,我有我所有的文件,它工作得很好,但问题是我如何能同时使用这两个按钮只有一个按钮工作得很好,如果我把它放在我的清单代码的第一位,就像我之前所说的,另一个没有工作的问题,只有在清单代码我尝试了代码,它工作对于第一个意图过滤器,不管它是MainActivity还是Mainsub@rothloup“我有我所有的xml和java文件”@mmorsy,清单不是代码。它不会决定按钮的行为。它所做的(在您的情况下)就是声明应用程序的组件(活动、接收者、服务等等)。仅仅因为您交换了清单中的顺序,并且获得了不同的行为,并不意味着解决方案就在您的清单中。如果您发布了所有的文件,那么您将丢失很多代码和布局,这些都是完成您描述的工作所必需的。我希望我的回答至少能帮你指出正确的方向。我的文件----rothloupok一些注释。首先,复习和复习,以便下次有人能更容易地帮助你。其次,您的“masaa”类没有为按钮设置侦听器的代码。第三,两个按钮似乎都是在同一个布局文件“content_main”中定义的。你的“main”类使用“content\u main”,因此它可以找到id为“main\u text\u view”的按钮,但是你的“sabaa”类找不到id为“masaa\u text\u view”的按钮,因为它位于另一个布局文件中。你的代码和布局文件的状态表明你对如何创建应用程序有很多困惑,特别是如何处理布局和意图。android开发人员文档有一个很好的小部分,正好涵盖了这个主题。我建议您遵循它,这样您就可以学习基础知识-这将比让别人为您修复代码更好。你也可以参考我答案中的细节来指导你。祝你好运。
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/main"
    android:background="@drawable/main"
    android:layout_centerHorizontal="true"
    android:id="@+id/main_text_view"


    android:layout_marginTop="17dp"


    />
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    addListenerOnButton();
}

public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.main_text_view);  //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate().  However, the id you specify doesn't exist in your layout file.  This should either not compile or return null.

//This is fine.
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, main.class);  //<--"main.class" doesn't match either of the activity names declared in your manifest.  It should match one of the names declared in the <name> tag of one of your <activity> tags.
            startActivity(intent);

        }

    });

}
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main"
android:background="@drawable/main"
android:layout_centerHorizontal="true"
android:id="@+id/main_text_view"    <!--  Here is the line that identifies the button for your app.  The format is "@+id/some_name", and is reference as "R.id.some_name" in your code. -->


android:layout_marginTop="17dp"


/>
button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Subactivity.class);
            startActivity(intent);

        }

    });
button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);

        }

    });