简体   繁体   中英

Moving from activity A to activity B and back from activity B to activity A

I'm very new to this and I've been looking around the forum for a while to get an answer to my question but I can't seem to get this working and I would very much appreciate help from your pros:)

As the title specifies I would like to go from activity A to activity B on a button click and from activity B on a button click go back to activity A.

My problem is first of all that I can't even go from activity A to activity B because the app crashes when I press the button. Why?

Second I would like to know if my code for returning from activity B to activity A is correct.

My code to go from activity A to activity B

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

    }

    public void ToCV(View v) {
        Intent intent = new Intent(this, CV.class);
    startActivity(intent);
}
}

And now the code for returning from Activity B to Activity A

public class CV extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cv);
    }
   
    public void GoBack(View v) {
      startActivity(new Intent(CV.this,MainActivity.class));

}
}

here is the crash log when i run the app##

2021-03-03 11:29:46.290 4104-4104/com.example.cvapp D/AndroidRuntime: Shutting down VM
2021-03-03 11:29:46.291 4104-4104/com.example.cvapp W/m.example.cvap: Accessing hidden method Lcom/msic/qarth/PatchStore;->createDisableExceptionQarthFile(Ljava/lang/Throwable;)Z (blacklist, JNI)
2021-03-03 11:29:46.291 4104-4104/com.example.cvapp E/m.example.cvap: [qarth_debug:]  get PatchStore::createDisableExceptionQarthFile method fail.
2021-03-03 11:29:46.296 4104-4104/com.example.cvapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cvapp, PID: 4104
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:6659)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:6631)
        at android.view.View.access$3100(View.java:790)
        at android.view.View$PerformClick.run(View.java:26187)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:6659) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:6631) 
        at android.view.View.access$3100(View.java:790) 
        at android.view.View$PerformClick.run(View.java:26187) 
        at android.os.Handler.handleCallback(Handler.java:907) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.cvapp/com.example.cvapp.CV}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2112)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1757)
        at android.app.Activity.startActivityForResult(Activity.java:4803)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:4743)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5164)
        at android.app.Activity.startActivity(Activity.java:5132)
        at com.example.cvapp.MainActivity.ToCV(MainActivity.java:21)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:6659) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:6631) 
        at android.view.View.access$3100(View.java:790) 
        at android.view.View$PerformClick.run(View.java:26187) 
        at android.os.Handler.handleCallback(Handler.java:907) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

The manifest

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

<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/Theme.CVapp">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Register your Activity B in the manifest file

Put these lines in your maifest

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

You should register Activity B in your AndroidManifest.xml Your manifest should look like this:

<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/Theme.CVapp">
    <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=".CV"/>

</application>

And also if you want to return from Activity B to Activity A, you can call method -> finish() - in Activity B and that will destroy Activity B and return to previous activity and that's Activity A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM