第一项活动仅适用于我的 Android 项目

我是 android 和 java 编程的新手,所以我尝试从几个项目中学习,但我遇到了这个问题。


当我尝试运行应用程序时,只有第一个活动在工作,而第二个活动不工作。


我在哪里错过了这个应用程序?


我很困惑,两个 java 项目正在运行,但唯一的第一个活动有效。


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.android_examples.wallpaper_android_examplescom.MainActivity">


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:src="@drawable/img76"

        android:layout_above="@+id/btn_right"

        android:layout_alignParentTop="true"

        android:layout_marginBottom="30dp"

        android:scaleType="fitXY" />


    <Button

        android:id="@+id/btn_right"

        style="@style/Widget.AppCompat.Button.Colored"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/btn_left"

        android:layout_alignBottom="@+id/btn_left"

        android:layout_alignParentEnd="true"

        android:layout_alignParentRight="true"

        android:layout_marginEnd="42dp"

        android:layout_marginRight="42dp"

        android:text="RIGHT" />


    <Button

        android:id="@+id/btn_left"

        style="@style/Widget.AppCompat.Button.Colored"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentStart="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentBottom="true"

        android:layout_marginStart="43dp"

        android:layout_marginLeft="43dp"

        android:layout_marginBottom="100dp"

        android:text="LEFT" />


</RelativeLayout>


慕森王
浏览 187回答 5
5回答

倚天杖

在您的点击侦听器中编写此代码:&nbsp; btn_right.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index = current_image_index % images.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(images[current_image_index]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(this, MainActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

红糖糍粑

你必须开始你的第二个活动startActivity(new Intent(getApplicationContext(), MainActivity.class));确保您在清单文件中添加了第二个活动

catspeake

没有代码可以导航到 MainActivity触发以下代码private void startMainActivity(){&nbsp; &nbsp; &nbsp;Intent intent = new Intent(this, MainActivity.class);&nbsp; &nbsp; &nbsp;startActivity(intent);}

收到一只叮咚

&nbsp;void SwitchButton() {&nbsp; &nbsp; btn_right = (Button) findViewById(R.id.btn_right);&nbsp; &nbsp; btn_left = (Button) findViewById(R.id.btn_left);&nbsp; &nbsp; btn_right.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index = current_image_index % images.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(images[current_image_index]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startMainPage();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; btn_left.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (current_image_index < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_image_index = images.length - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(images[current_image_index]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startMainPage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );}private void startMainPage() {&nbsp; &nbsp; Intent intent = new Intent(Login.this, MainActivity.class);&nbsp; &nbsp; startActivity(intent);}

狐的传说

无论您想在哪里调用下一个活动,请使用:-&nbsp; Intent intent = new Intent(this, MainActivity.class);&nbsp; startActivity(intent);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java