我在初始活動中有兩個按鈕,點擊時每個按鈕都需要移動到不同的活動。第一個按鈕似乎工作正常,但第二個按鈕只是停止整個應用程序。我試着實現這裏發佈的所有解決方案,但似乎沒有任何工作。如何在android中執行同一活動中的兩個按鈕?
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 /過程:發送信號。 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:Use EGL_SWAP_BEHAVIOR_PRESERVED:true 03-25 22:25:34.917 977-1423/com.example.fryselectronics.knowyourbooks我/ Adreno-EGL :: QUALCOMM Build:10/09/15,6cbbf7d,I3193f6e94a 03 -25 22:25:34.921 977-1423/com.example.fryselectronics.knowy ourbooks 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:位於0xb8fab560(RippleDrawable)上的帶有句柄的endAllActiveAnimators 0xb8e11eb0 03-25 22:25:39.101 977-1423/com.example.fryselectronics.knowyourbooks E/Surface:getSlotFromBufferLocked:unknown緩衝區:0xb8fbb720 03-25 22:25:39.101 977-1423/com.example.fryselectronics.knowyourbooks D/OpenGLRenderer:位於0xb8ec5cf0(RippleDrawable)上的帶有句柄0xb8e51df0的endAllActiveAnimators 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-97 7/com.example.fryselectronics。knowyourbooks d/AndroidRuntime:關閉VM
一些評論:1)你有兩個方法變量,'intent'和'intentt'映射兩個具有相同名稱的類成員變量。這是一個等待發生的錯誤。使用類或方法變量。 2)你的變量名非常接近,很容易錯過「intentt」中的第二個「t」。 3)如果清理變量不起作用,請檢查它是否是導致問題的活動。例如,您可以交換按鈕上的活動來查看問題是否存在。 – Cheticamp
「Main2Activity」和「Main6Activity」活動實際上存在於您的應用程序中嗎?如果是這樣,你可以發佈他們的代碼嗎?日誌表明它找不到'Main6Activity'類。 –
@Cheticamp Main6Activity似乎存在一個問題。 – Anon