Android Studio:Activity 在所有编程项目中使用 Intents 时未发现异常
Android Studio: Activity not found exception when using Intents in ALL programming projects
一个月后开始我的项目后,我 运行 陷入了一个真正灾难性的问题:我不能再开始新的活动了!每次我使用意图执行此操作时,我都会收到 Activity 未找到错误,并且 Android 工作室希望我在清单中声明我的活动。但是,我已经检查了几年的声明,但不知道问题出在哪里。我从头开始创建了一个(多个,而不是)新项目以避免任何并发症,但问题仍然存在。除其他外,我尝试过这样声明活动
android:name=".MainActivity"
像这样
android:name="com.example.dorothea.intenttest.MainActivity"
并像这样制定意图
public final Intent intenttest = new Intent(this, SecondActivity.class);
还有这个
public final Intent intenttest = new Intent(MainActivity.this, SecondActivity.class);
此外,因为这个问题即使在最简单的项目中仍然存在,所以我想不出在开始的 activity 的构造中有一个空指针异常让我逃脱了。我真的很困惑,任何帮助将不胜感激。可能这对我来说是一个非常愚蠢的错误,但我看不出是哪一个 :(
在下文中,我附上 logcat 和我用来查找问题的一个非常简单的(两个空白页面)应用程序的代码。我让 Android Studio 创建了两个空白活动,并在第一个 Java 文件中添加了两行代码。
Logcat:
08-24 13:36:32.460 26726-26726/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dorothea.intenttest/com.example.dorothea.intenttest .MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access0(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
at android.app.Activity.startActivityForResult(Activity.java:3434)
at android.app.Activity.startActivityForResult(Activity.java:3395)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:3630)
at android.app.Activity.startActivity(Activity.java:3598)
at com.example.dorothea.intenttest.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access0(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
08-24 13:36:39.137 26726-26726/? I/Process﹕ Sending signal. PID: 26726 SIG: 9
Java先归档activity:
package com.example.dorothea.intenttest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public final Intent intenttest = new Intent(this, SecondActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(intenttest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
要启动的activity就像android创建的一样:
package com.example.dorothea.intenttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
不用说,Android Studio 实际上已经在清单文件中声明了活动,清单文件在这里:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dorothea.intenttest" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
在 activity 生命周期中初始化之前,您不能使用上下文。可以在 onCreate()
之后创建 Intent
将此行移动到侦听器内部或当 activity 为 运行 时调用的某些方法(在使用前创建它):
public final Intent intenttest = new Intent(this, SecondActivity.class);
错误在这行代码
public final Intent intenttest = new Intent(this, SecondActivity.class);
您必须将该初始化移动到 onCreate
或其他一些 activity 方法中。
您必须将 Activity 实例传递给 Intent
构造函数,而该实例尚未创建。
您应该使用您的包在清单中声明 activity。在你的情况下:
<activity
android:name=".intenttest.SecondActivity"
android:label="@string/title_activity_second" >
一个月后开始我的项目后,我 运行 陷入了一个真正灾难性的问题:我不能再开始新的活动了!每次我使用意图执行此操作时,我都会收到 Activity 未找到错误,并且 Android 工作室希望我在清单中声明我的活动。但是,我已经检查了几年的声明,但不知道问题出在哪里。我从头开始创建了一个(多个,而不是)新项目以避免任何并发症,但问题仍然存在。除其他外,我尝试过这样声明活动
android:name=".MainActivity"
像这样
android:name="com.example.dorothea.intenttest.MainActivity"
并像这样制定意图
public final Intent intenttest = new Intent(this, SecondActivity.class);
还有这个
public final Intent intenttest = new Intent(MainActivity.this, SecondActivity.class);
此外,因为这个问题即使在最简单的项目中仍然存在,所以我想不出在开始的 activity 的构造中有一个空指针异常让我逃脱了。我真的很困惑,任何帮助将不胜感激。可能这对我来说是一个非常愚蠢的错误,但我看不出是哪一个 :( 在下文中,我附上 logcat 和我用来查找问题的一个非常简单的(两个空白页面)应用程序的代码。我让 Android Studio 创建了两个空白活动,并在第一个 Java 文件中添加了两行代码。
Logcat:
08-24 13:36:32.460 26726-26726/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dorothea.intenttest/com.example.dorothea.intenttest .MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access0(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
at android.app.Activity.startActivityForResult(Activity.java:3434)
at android.app.Activity.startActivityForResult(Activity.java:3395)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:3630)
at android.app.Activity.startActivity(Activity.java:3598)
at com.example.dorothea.intenttest.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access0(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
08-24 13:36:39.137 26726-26726/? I/Process﹕ Sending signal. PID: 26726 SIG: 9
Java先归档activity:
package com.example.dorothea.intenttest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public final Intent intenttest = new Intent(this, SecondActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(intenttest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
要启动的activity就像android创建的一样:
package com.example.dorothea.intenttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
不用说,Android Studio 实际上已经在清单文件中声明了活动,清单文件在这里:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dorothea.intenttest" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
在 activity 生命周期中初始化之前,您不能使用上下文。可以在 onCreate()
将此行移动到侦听器内部或当 activity 为 运行 时调用的某些方法(在使用前创建它):
public final Intent intenttest = new Intent(this, SecondActivity.class);
错误在这行代码
public final Intent intenttest = new Intent(this, SecondActivity.class);
您必须将该初始化移动到 onCreate
或其他一些 activity 方法中。
您必须将 Activity 实例传递给 Intent
构造函数,而该实例尚未创建。
您应该使用您的包在清单中声明 activity。在你的情况下:
<activity
android:name=".intenttest.SecondActivity"
android:label="@string/title_activity_second" >