如何在android中实现同一个activity中的两个按钮?
How to implement two buttons in the same activity in android?
我的初始 activity 中有两个按钮,每个按钮在单击时都需要移动到不同的 activity。第一个按钮似乎工作正常,但第二个按钮只是停止了整个应用程序。我已经尝试实施此处发布的所有解决方案,但似乎没有任何效果。
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.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: 类加载器引用未知路径: /data/app/com.example.fryselectronics.knowyourbooks-2/lib/arm
03-25 22:25:34.625 977-983/com.example.fryselectronics.knowyourbooks W/art:挂起所有线程耗时:13.958 毫秒
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: 未知缓冲区: 0xb8e4e140
03-25 22:25:37.933 977-1423/com.example.fryselectronics.knowyourbooks D/OpenGLRenderer:0xb8fab560 (RippleDrawable) 上的 endAllActiveAnimators,句柄为 0xb8e11eb0
03-25 22:25:39.101 977-1423/com.example.fryselectronics.knowyourbooks E/Surface:getSlotFromBufferLocked:未知缓冲区: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: 未知缓冲区: 0xb8e613a8
03-25 22:25:44.821 977-977/com.example.fryselectronics.knowyourbooks D/AndroidRuntime: 关闭虚拟机
你好尝试改变这个:
<activity android:name=".Main2Activity" />
<activity android:name=".Main6Activity"></activity>
通过 Refactoring Class 名称在你的清单中做到这一点:
<activity android:name=".MainActivity_Two" />
<activity android:name=".MainActivity_Six"></activity>
命名可能有问题
或者尝试在您的 MainActivity.java
class
中更改此设置
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);
}
});
这是您要制作的作品的工作版本:
主要Activity
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);
}
});
}
}
和 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
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"
tools:context="com.ngenh.stack.MainActivity">
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="67dp"
android:layout_marginStart="67dp"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp"
android:layout_marginTop="107dp"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
另外两个活动是空的。
对您的代码的一些评论:
- 尽量不要像内置变量那样命名变量,因为您会隐藏它们。
- 如果您发现相同的代码重复出现,唯一不同的是一两个变量,请尝试为其创建一个函数。
- 提供具体的名称和 ID(与我所做的不同)。登录按钮应该是 btnLogin 或类似的东西,而不是 button1、button2 等。
根据您附加的错误日志,您的 AndroidManifest.xml 文件似乎没有问题。再次 clean
和 build
您的项目。
顺便说一句,我已经修改了你的一些代码部分并且它工作正常。你可以试试这个:
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>
我的初始 activity 中有两个按钮,每个按钮在单击时都需要移动到不同的 activity。第一个按钮似乎工作正常,但第二个按钮只是停止了整个应用程序。我已经尝试实施此处发布的所有解决方案,但似乎没有任何效果。
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.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: 类加载器引用未知路径: /data/app/com.example.fryselectronics.knowyourbooks-2/lib/arm 03-25 22:25:34.625 977-983/com.example.fryselectronics.knowyourbooks W/art:挂起所有线程耗时:13.958 毫秒 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: 未知缓冲区: 0xb8e4e140 03-25 22:25:37.933 977-1423/com.example.fryselectronics.knowyourbooks D/OpenGLRenderer:0xb8fab560 (RippleDrawable) 上的 endAllActiveAnimators,句柄为 0xb8e11eb0 03-25 22:25:39.101 977-1423/com.example.fryselectronics.knowyourbooks E/Surface:getSlotFromBufferLocked:未知缓冲区: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: 未知缓冲区: 0xb8e613a8 03-25 22:25:44.821 977-977/com.example.fryselectronics.knowyourbooks D/AndroidRuntime: 关闭虚拟机
你好尝试改变这个:
<activity android:name=".Main2Activity" />
<activity android:name=".Main6Activity"></activity>
通过 Refactoring Class 名称在你的清单中做到这一点:
<activity android:name=".MainActivity_Two" />
<activity android:name=".MainActivity_Six"></activity>
命名可能有问题
或者尝试在您的 MainActivity.java
class
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);
}
});
这是您要制作的作品的工作版本:
主要Activity
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); } }); }
}
和 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
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"
tools:context="com.ngenh.stack.MainActivity">
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="67dp"
android:layout_marginStart="67dp"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp"
android:layout_marginTop="107dp"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
另外两个活动是空的。
对您的代码的一些评论:
- 尽量不要像内置变量那样命名变量,因为您会隐藏它们。
- 如果您发现相同的代码重复出现,唯一不同的是一两个变量,请尝试为其创建一个函数。
- 提供具体的名称和 ID(与我所做的不同)。登录按钮应该是 btnLogin 或类似的东西,而不是 button1、button2 等。
根据您附加的错误日志,您的 AndroidManifest.xml 文件似乎没有问题。再次 clean
和 build
您的项目。
顺便说一句,我已经修改了你的一些代码部分并且它工作正常。你可以试试这个:
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>