一、关于安卓的启动
1、安卓的一个主要的xml配置文件是:AndroidManifest.xml
该文件放在应用的根目录,那么具体根目录是哪里需要根据各个项目去确定(不是工程的根目录,可能是对应的app的src->main->的目录下面,具体根据各个工程的设置)
该文件中的application tag下面可以放置activity并且可以有多个activity,但是每个activity对应的android:name均要不一致
<application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.SystemWidget" tools:targetApi="31"> <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> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> <activity android:name=".MyViewTest" /> <activity android:name=".TopBarTest" /> </application>
如上所示,有3个activity,但是第一个比较特殊还有intent-filter, 对应的action是MAIN表明程序将会以MainActivity类先启动,对应的category是LAUNCHER指明了该应用的图标,那么如果把该launcher去掉会发生什么事情呢?看到有别的博客说是可以启动但是没有图标,这里尝试了一下发现报了如下的错误:Could not identify launch activity: Default Activity not found
二、关于activity在学习过程中遇到的问题
1、编写了类class TopBarTest extends Activity, 然后在类MainActivity中进行调用:startActivity(new Intent(this, TopBarTest.class)); 发现报错,报了如下的错误:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.agiledeveloper.systemwidget, PID: 26002 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446) at android.view.View.performClick(View.java:7317) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) at android.view.View.performClickInternal(View.java:7291) at android.view.View.access$3600(View.java:838) at android.view.View$PerformClick.run(View.java:28247) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8668) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) at android.view.View.performClick(View.java:7317) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) at android.view.View.performClickInternal(View.java:7291) at android.view.View.access$3600(View.java:838) at android.view.View$PerformClick.run(View.java:28247) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8668) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109) Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.agiledeveloper.systemwidget/com.agiledeveloper.systemwidget.TopBarTest}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2118) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1746) at android.app.Activity.startActivityForResult(Activity.java:5412) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:712) at android.app.Activity.startActivityForResult(Activity.java:5337) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:693) at android.app.Activity.startActivity(Activity.java:5819) at android.app.Activity.startActivity(Activity.java:5787) at com.agiledeveloper.systemwidget.MainActivity.btnTopBar(MainActivity.java:36) at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) at android.view.View.performClick(View.java:7317) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) at android.view.View.performClickInternal(View.java:7291) at android.view.View.access$3600(View.java:838) at android.view.View$PerformClick.run(View.java:28247) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8668) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)View Code
刚开始看了错误前面几行,实在是看不出为啥崩溃,后来看到这一行错误提示时候:Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.agiledeveloper.systemwidget/com.agiledeveloper.systemwidget.TopBarTest}; have you declared this activity in your AndroidManifest.xml?
才发现原来继承Activity,使用Activity需要在AndroidManifest.xml中添加对应的Activity,即上面的xml中的最后一行
标签:java,启动,os,安卓,activity,android,com,View From: https://www.cnblogs.com/czwlinux/p/16964828.html