Java Android studio按钮将我带到错误的活动

Java Android studio按钮将我带到错误的活动,java,android-studio,android-layout,android-manifest,Java,Android Studio,Android Layout,Android Manifest,我是安卓工作室的新手,这是我的第二个项目。我最近遇到了一个问题,最近当我运行我的应用程序时,当我单击主菜单活动上的create_account按钮时,出现了这个问题。出于某种原因,它将我发送到我的ViewlogActivity。当我点击我的Viewlogs按钮时,它什么也不做。我仔细检查了我的意图,并在清单文件中包含了这两个活动。发生什么事了 我的代码: Manifest File:

我是安卓工作室的新手,这是我的第二个项目。我最近遇到了一个问题,最近当我运行我的应用程序时,当我单击主菜单活动上的create_account按钮时,出现了这个问题。出于某种原因,它将我发送到我的ViewlogActivity。当我点击我的Viewlogs按钮时,它什么也不做。我仔细检查了我的意图,并在清单文件中包含了这两个活动。发生什么事了

我的代码:

Manifest File:                                                     
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                s<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<!--        <activity-->
<!--        android:name=".CreateAccountActivity"-->
<!--        android:theme="@style/AppTheme.NoActionBar">-->
<!--        <intent-filter>-->
<!--            <action android:name="android.intent.action.MAIN" />-->

<!--            <category android:name="android.intent.category.DEFAULT" />-->
<!--        </intent-filter>-->
<!--    </activity>-->

        <activity
            android:name=".CreateAccountActivity2"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ViewLogActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <activity
            android:name=".LoginActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LogoutActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>





**Main Activity Buttons:** 

Button create_account_button = findViewById(R.id.create_account);
        create_account_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // call the create account activity
                Log.d("MainActivity", "onClick for create account called");
                Intent intent = new Intent(MainActivity.this, CreateAccountActivity2.class);
                startActivity(intent);

            }
        });


        Button login_button = findViewById(R.id.login);
        login_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // call the login Activity
                Log.d("Login", "onClick for login activity called");
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);

            }
        });



        Button logout_button = findViewById(R.id.logout);
        logout_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // call the logout Activity
                Log.d("Logout", "onClick for logout activity called");
                Intent intent = new Intent(MainActivity.this, LogoutActivity.class);
                startActivity(intent);

            }
        });

        Button view_logs_button = findViewById(R.id.viewlogs);
        create_account_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // call the view log activity
                Log.d("MainActivity", "onClick for view log called");
                Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
                startActivity(intent);

            }
        });


        Button exit_button = findViewById(R.id.exit);
        exit_button.setOnClickListener(new View.OnClickListener(){
            // call to exit the application
            @Override
            public void onClick(View v) {
                Log.d("Exit", "onClick for exit called");
                finish();

            }
        });


**Main Activity XML layout File:** 

<TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scaleX="2"
        android:scaleY="2"
        android:gravity="center"
        android:text="Main Menu"/>


    <Button
        android:id="@+id/create_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Create Account" />

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:gravity="center_horizontal"
        />



    <Button
        android:id="@+id/logout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Logout"
        android:gravity="center_horizontal"
        />

    <Button
        android:id="@+id/viewlogs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="View Logs" />

    <Button
        android:id="@+id/exit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:gravity="center_horizontal"
        />
清单文件:
s
**主要活动按钮:**
Button create_account_Button=findViewById(R.id.create_account);
创建帐户按钮。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用创建帐户活动
Log.d(“MainActivity”,“onClick for create account called”);
意向意向=新意向(MainActivity.this、CreateAccountActivity2.class);
星触觉(意向);
}
});
按钮登录\按钮=findViewById(R.id.login);
login_button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用登录活动
d(“Login”,“onClick用于调用的登录活动”);
意向意向=新意向(MainActivity.this、LoginActivity.class);
星触觉(意向);
}
});
按钮注销\按钮=findViewById(R.id.logout);
注销按钮。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用注销活动
Log.d(“注销”,“为注销活动调用onClick”);
意向意向=新意向(MainActivity.this、LogoutActivity.class);
星触觉(意向);
}
});
按钮视图\日志\按钮=findViewById(R.id.viewlogs);
创建帐户按钮。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用查看日志活动
d(“MainActivity”,“onClick用于调用视图日志”);
意向意向=新意向(MainActivity.this、ViewLogActivity.class);
星触觉(意向);
}
});
按钮退出按钮=findViewById(R.id.exit);
退出按钮.setOnClickListener(新视图.OnClickListener(){
//调用以退出应用程序
@凌驾
公共void onClick(视图v){
Log.d(“Exit”,“onClick用于调用Exit”);
完成();
}
});
**主活动XML布局文件:**
问题在这里-

您正在获取
查看日志按钮的id
,但在
创建帐户按钮上应用了click(您在
创建帐户按钮上应用了两次clicl监听器,因此它是最近一次点击监听器),并重定向到
查看日志活动

Button view_logs_button = findViewById(R.id.viewlogs);
create_account_button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        // call the view log activity
        Log.d("MainActivity", "onClick for view log called");
        Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
        startActivity(intent);

    }
});
要解决此问题,请单击下面的“查看日志”按钮上的侦听器-

Button view_logs_button = findViewById(R.id.viewlogs);
view_logs_button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        // call the view log activity
        Log.d("MainActivity", "onClick for view log called");
        Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
        startActivity(intent);

    }
});
希望这对你有帮助