【问题标题】:Error while Launching activity in android studio using empty activity使用空活动在 android studio 中启动活动时出错
【发布时间】:2019-08-13 14:05:12
【问题描述】:

我刚刚做了一个最小的应用程序,我遇到了解决它的问题。 运行后出现此错误。 " 无法识别启动活动:未找到默认活动 启动活动时出错“

我尝试编辑 Manifest 文件并添加这样的主活动包:“com.example.loanapplication.MainActivity”并编辑了配置并“指定了活动”,但运行后出现了同样的错误。

主活动

public class MainActivity extends AppCompatActivity {

    private ListView listViewLoans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try{
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            List<Loans> loans = new HttpRequestLoansList().execute().get();
            listViewLoans = (ListView) findViewById(R.id.listViewLoans);
            listViewLoans.setAdapter( new LoanListAdapter(loans, getApplicationContext()));
        }
        catch (Exception e){
            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
            builder.setMessage(e.getMessage());
            builder.create().show();
        }

    }

    private  class HttpRequestLoansList extends AsyncTask<Void, Void, List<Loans>>{

        @Override
        protected List<Loans> doInBackground(Void... params) {
            LoanModel loanModel = new LoanModel();
            return  loanModel.all();
        }

        @Override
        protected void onPostExecute(List<Loans> loans) {
            super.onPostExecute(loans);
        }
    }

清单文件

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


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <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/AppTheme">
        <activity android:name="com.example.loanapplication.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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




    </manifest>

activity_main xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Loan" />

    <ListView
        android:id="@+id/listViewLoans"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

错误包括以下内容: 无法识别启动活动:未找到默认活动 启动活动时出错

期望:

【问题讨论】:

  • 先猜想把 改成
  • @noname 没有理由这样做,因为这两个声明本质上是相同的。
  • 您的清单和代码看起来不错。这听起来像是一个 Android Studio 问题,它(出于某种原因)找不到要启动的 Activity
  • @DavidWasser 不确定您的意思。我认为 android 在 com.example.loanapplication.com.example.loanapplication 中寻找 MainActivity 时会感到困惑。包(是的,它在嵌套包中查找)
  • 但你也可以查看这个帖子stackoverflow.com/questions/33855518/…

标签: android


【解决方案1】:

请转到 Build > clean project 然后重新构建它

【讨论】:

  • 你们是对的。我之前遇到过这个问题,建议的解决方案是我做过的一件事,但这并不能解决问题。我认为对我来说,当我切换项目然后我必须清理项目并构建它时会发生这种情况。因为我认为当我更改项目时缓存问题和 android 会混淆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2016-07-07
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多