Android кнопка студии, приводящая меня к неправильной активности - PullRequest
0 голосов
/ 07 февраля 2020

Я относительно новичок в Android Studio, это мой второй проект. В настоящее время у меня возникает проблема, которая возникла совсем недавно, когда я запускаю свое приложение, когда я нажимаю кнопку 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"
        />

1 Ответ

0 голосов
/ 07 февраля 2020

Проблема здесь -

вы получаете идентификатор view_logs_button, но нажимаете на create_account_button (вы применяете прослушиватель кликов на create_account_button 2 раза, так что он принимает прослушиватель последних нажатий) и перенаправляете на ViewLogActivity.

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);

    }
});

Чтобы решить эту проблему, примените кнопку прослушивания на view_logs_button, как показано ниже -

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);

    }
});

Надеюсь, это поможет вам !!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...