我对Android Studio比较陌生,这是我的第二个项目。我目前遇到了一个问题,当我运行我的应用程序时,当我单击主菜单活动上的create_account按钮时,这个问题最近才出现。出于某种原因,它将我发送到我的ViewlogActivity。当我单击我的查看日志按钮时,它什么也不做。 我仔细检查了我的意图,并在我的清单文件中进行了这两项活动。这是怎么回事?
我的代码:
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"
/>
问题在这里 -
您获得的 ID 为view_logs_button
但应用 单击create_account_button
(您在create_account_button
上应用 CLICL 侦听器 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);
}
});
希望这对你有帮助!!