首页 > 解决方案 > 尝试运行 appium 但应用程序自动关闭并显示错误

问题描述

我是新来的 appium 并尝试打开 android 模拟器的设置。我已经安装了 appium 1.21.0 并使用 android studio 创建了一个测试用例。测试完成后,我看到的是它关闭了应用程序。我在 android 模拟器中运行该应用程序。nmnfmds kjbsfjkhdsd hkjhf jkshfjkhs kbkjfsd kjbsdfjkb kjbkjsd。

这是我的MainActivity.java文件


import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class MainActivity {
    static AndroidDriver<MobileElement> driver;
    private static URL url;

    public static void main(String[] args){
        openpi();
    }


 public static void  openpi(){

      DesiredCapabilities cap = new DesiredCapabilities();

      cap.setCapability("deviceName","sdk_gphone_x86");
      cap.setCapability("platformName","Android");
      cap.setCapability("udid","emulator-5556");
      cap.setCapability("platformVersion","11");
      cap.setCapability("appPackage","com.android.settings");
      cap.setCapability("appActivity", "com.android.settings.homepage.SettingsHomepageActivity");
      cap.setCapability("appWaitDuration","2000");


     try {
         url = new URL("http://127.0.0.1:4723/wd/hub");
     } catch (MalformedURLException e) {
         e.printStackTrace();
     }
     driver = new AndroidDriver<MobileElement>(url, cap);
 }
    }```

**This error is showing when i run app......**

`08/10 23:40:48: Launching 'app' on Pixel 4 API 30.Install successfully finished in 82 ms.
$ adb shell am start -n "com.example.appium_automation/com.example.appium_automation.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER

Connected to process 22983 on device 'Pixel_4_API_30 [emulator-5556]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/chatty: uid=10155(com.example.appium_automation) re-initialized> identical 1 line
W/pium_automatio: Accessing hidden field Landroid/app/ResourcesManager$ActivityResources;->overrideConfig:Landroid/content/res/Configuration; (greylist-max-o, linking, denied)
    Accessing hidden method Landroid/content/res/Configuration;->diffPublicOnly(Landroid/content/res/Configuration;)I (greylist-max-o, linking, denied)
    Accessing hidden method Landroid/app/ResourcesManager;->overrideTokenDisplayAdjustments(Landroid/os/IBinder;Ljava/util/function/Consumer;)Z (blacklist, linking, denied)
    Accessing hidden field Landroid/app/ResourcesManager$ActivityResources;->activityResources:Ljava/util/ArrayList; (greylist-max-o, linking, denied)
    Accessing hidden method Landroid/content/res/Resources;->overrideDisplayAdjustments(Ljava/util/function/Consumer;)V (blacklist, linking, denied)
    Accessing hidden method Landroid/app/ResourcesManager;->updateResourcesForActivity(Landroid/os/IBinder;Landroid/content/res/Configuration;IZ)V (greylist-max-o, linking, denied)
    Accessing hidden method Landroid/app/ResourcesManager;->getOrCreateActivityResourcesStructLocked(Landroid/os/IBinder;)Landroid/app/ResourcesManager$ActivityResources; (greylist-max-o, linking, denied)
W/pium_automatio: Redefining intrinsic method java.lang.Thread java.lang.Thread.currentThread(). This may cause the unexpected use of the original definition of java.lang.Thread java.lang.Thread.currentThread()in methods that have already been compiled.
W/pium_automatio: Redefining intrinsic method boolean java.lang.Thread.interrupted(). This may cause the unexpected use of the original definition of boolean java.lang.Thread.interrupted()in methods that have already been compiled.
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
    No Network Security Config specified, using platform default
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appium_automation, PID: 22983
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.appium_automation/com.example.appium_automation.MainActivity}: java.lang.ClassCastException: com.example.appium_automation.MainActivity cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.ClassCastException: com.example.appium_automation.MainActivity cannot be cast to android.app.Activity
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
W/System: A resource failed to call close. 
`
**Here is my `Androidmanifest.xml` file**


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

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

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

</manifest>```

标签: javaandroidselenium-webdriverappiumappium-android

解决方案


推荐阅读