如何在android中的同一个活动中实现两个按钮?

时间:2017-03-25 16:53:12

标签: java android android-activity android-button

我的初始活动中有两个按钮,点击时每个按钮都需要移动到另一个活动。第一个按钮似乎工作正常,但第二个按钮只是暂停整个应用程序。我已尝试实施此处发布的所有解决方案,但似乎没有任何效果。

MainActivity.java

package com.example.fryselectronics.knowyourbooks;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button ab;
Button ba;
Intent intent;
Intent intentt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);







    ab=(Button)findViewById(R.id.button2);
    final Intent intent= new Intent();
    intent.setClass(MainActivity.this, Main2Activity.class);

    ab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            startActivity(intent);

        }

    });
    ba=(Button)findViewById(R.id.button);
    final Intent intentt= new Intent();
    intentt.setClass(MainActivity.this, Main6Activity.class);

    ba.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


                    startActivity(intentt);


            }

    });

}

}

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/a"
tools:context="com.example.fryselectronics.knowyourbooks.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Click on any one of the data sets from below"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B"
    android:id="@+id/button"
    android:layout_centerVertical="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="A"
    android:id="@+id/button2"
    android:layout_above="@+id/button"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="40dp"
    android:layout_alignEnd="@+id/button" />

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fryselectronics.knowyourbooks">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".Main6Activity"></activity>
</application>

日志:

03-25 22:25:24.410 32470-32470/com.example.fryselectronics.knowyourbooks E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.fryselectronics.knowyourbooks, PID: 32470
                                                                                       android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.fryselectronics.knowyourbooks/com.example.fryselectronics.knowyourbooks.Main6Activity}; have you declared this activity in your AndroidManifest.xml?
                                                                                           at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1885)
                                                                                           at android.app.Instrumentation.execStartActivity(Instrumentation.java:1579)
                                                                                           at android.app.Activity.startActivityForResult(Activity.java:3921)
                                                                                           at android.app.Activity.startActivityForResult(Activity.java:3881)
                                                                                           at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
                                                                                           at android.app.Activity.startActivity(Activity.java:4208)
                                                                                           at android.app.Activity.startActivity(Activity.java:4176)
                                                                                           at com.example.fryselectronics.knowyourbooks.MainActivity$2.onClick(MainActivity.java:51)
                                                                                           at android.view.View.performClick(View.java:5201)
                                                                                           at android.view.View$PerformClick.run(View.java:21163)
                                                                                           at android.os.Handler.handleCallback(Handler.java:746)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)

at.com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

03-25 22:25:26.248 32470-32470 / com.example.fryselectronics.knowyourbooks I / Process:发送信号。 PID:32470 SIG:9 03-25 22:25:27.195 977-977 / com.example.fryselectronics.knowyourbooks W / System:ClassLoader引用未知路径:/data/app/com.example.fryselectronics.knowyourbooks-2/lib/arm 03-25 22:25:33.868 977-977 / com.example.fryselectronics.knowyourbooks W / System:ClassLoader引用未知路径:/data/app/com.example.fryselectronics.knowyourbooks-2/lib/arm 03-25 22:25:34.625 977-983 / com.example.fryselectronics.knowyourbooks W / art:暂停所有线程:13.958ms 03-25 22:25:34.754 977-1423 / com.example.fryselectronics.knowyourbooks D / OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true 03-25 22:25:34.917 977-1423 / com.example.fryselectronics.knowyourbooks I / Adreno-EGL :: QUALCOMM Build:10/09 / 15,6cbbf7d,I3193f6e94a 03-25 22:25:34.921 977-1423 / com.example.fryselectronics.knowyourbooks I / OpenGLRenderer:初始化的EGL,版本1.4 03-25 22:25:37.932 977-1423 / com.example.fryselectronics.knowyourbooks E / Surface:getSlotFromBufferLocked:unknown buffer:0xb8e4e140 03-25 22:25:37.933 977-1423 / com.example.fryselectronics.knowyourbooks D / OpenGLRenderer:endAllActiveAnimators on 0xb8fab560(RippleDrawable)with handle 0xb8e11eb0 03-25 22:25:39.101 977-1423 / com.example.fryselectronics.knowyourbooks E / Surface:getSlotFromBufferLocked:unknown buffer:0xb8fbb720 03-25 22:25:39.101 977-1423 / com.example.fryselectronics.knowyourbooks D / OpenGLRenderer:endAllActiveAnimators on 0xb8ec5cf0(RippleDrawable)with handle 0xb8e51df0 03-25 22:25:41.803 977-1423 / com.example.fryselectronics.knowyourbooks E / Surface:getSlotFromBufferLocked:unknown buffer:0xb8e613a8 03-25 22:25:44.821 977-977 / com.example.fryselectronics.knowyourbooks D / AndroidRuntime:关闭虚拟机

3 个答案:

答案 0 :(得分:0)

这是您正在尝试制作的工作版本:

  1. 主要活动

    Error:(15, 10) error: android.support.v4.media.session.MediaControllerCompat is not nullable, 
    but is being provided by @Provides @...di.PerMediaSession @android.support.annotation.Nullable android.support.v4.media.session.MediaControllerCompat 
    ...di.MediaSessionModule.provideMediaController$app_debug(android.content.Context, android.support.v4.media.session.MediaSessionCompat)
    at:     <...where it is injected...>
    

    }

  2. 和xml:

    public class MainActivity extends AppCompatActivity {
    
    private Button btn1;
    private Button btn2;
    
    private void startMyActivity(Intent intent) {
        startActivity(intent);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btn1 = (Button) findViewById(R.id.button);
        btn2 = (Button) findViewById(R.id.button2);
    
    
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
                startMyActivity(myIntent);
            }
        });
    
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(MainActivity.this, Main3Activity.class);
                startActivity(myIntent);
            }
        });
    }
    

    其他两项活动都是空的。

    对您的代码的一些评论:

    1. 尽量不要像内置变量那样命名变量,因为你会影响它们。
    2. 如果您看到相同的代码重复,唯一的区别是变量或两个变量,请尝试为其创建一个函数。
    3. 提供特定的名称和ID(与我的不同)。登录按钮应该是btnLogin或类似的东西,而不是button1,button2等

答案 1 :(得分:0)

根据您附加的错误日志,您的AndroidManifest.xml文件似乎没问题。只需cleanbuild您的项目。

顺便说一句,我修改了你的一些代码部分,并且工作正常。你可以试试这个:

MainActivity.java

package com.example.fryselectronics.knowyourbooks;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonA;
    Button buttonB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonA = (Button) findViewById(R.id.button_a);
        buttonB = (Button) findViewById(R.id.button_b);

        buttonA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });
        buttonB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, Main6Activity.class);
                startActivity(intent);
            }
        });
    }
}

<强> Main2Activity.java

package com.example.fryselectronics.knowyourbooks;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

<强> Main6Activity.java

package com.example.fryselectronics.knowyourbooks;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main6Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
    }
}

<强> 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/a"
    tools:context="com.example.fryselectronics.knowyourbooks.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Click on any one of the data sets from below"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="A"
        android:layout_marginBottom="40dp"
        style="?android:attr/buttonStyleSmall" />

    <Button
        android:id="@+id/button_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_below="@id/button_a"
        android:text="B"
        style="?android:attr/buttonStyleSmall" />

</RelativeLayout>

<强> activity_main2.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:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.fryselectronics.knowyourbooks.Main2Activity">

</RelativeLayout>

<强> activity_main6.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:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.fryselectronics.knowyourbooks.Main6Activity">

</RelativeLayout>

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fryselectronics.knowyourbooks">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity" />
        <activity android:name=".Main6Activity" />
    </application>

</manifest>

答案 2 :(得分:-1)

您好尝试更改此内容:

<activity android:name=".Main2Activity" />

    <activity android:name=".Main6Activity"></activity>
通过重构类名称

来显示您的清单中的内容:

<activity android:name=".MainActivity_Two" />

    <activity android:name=".MainActivity_Six"></activity>

命名可能存在问题

或者尝试在MainActivity.java课程

中更改此内容
ab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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

        }

    });
    ba=(Button)findViewById(R.id.button);



    ba.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                   Intent intentt = new Intent(MainActivity.this, Main6Activity.class);
                    startActivity(intentt);


            }

    });

代替:

ab=(Button)findViewById(R.id.button2);
    final Intent intent= new Intent();
    intent.setClass(MainActivity.this, Main2Activity.class);

    ab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            startActivity(intent);

        }

    });
    ba=(Button)findViewById(R.id.button);
    final Intent intentt= new Intent();
    intentt.setClass(MainActivity.this, Main6Activity.class);

    ba.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


                    startActivity(intentt);


            }

    });