Android startActivity异常

时间:2016-07-17 17:42:58

标签: android android-intent android-activity android-manifest

我正试图通过fab按钮从MainActivity开始活动。我的代码如下。我得到的例外是:

ActivityNotFoundException: Unable to find explicit activity class //packagename; have you declared this activity in your AndroidManifest.xml?

虽然异常指向我的android清单,但我确实在那里添加了活动,尽管可能不正确。这是我的代码:

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    /*
    *  OnLongClick - start listening via Listen class
    * */
    fab.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            Intent intent = new Intent(getApplicationContext(), Listen.class);

            Snackbar.make(view, "Bluetooth authentication is on", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            startActivity(intent);
            return true;
        }
    });

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Bluetooth authentication turned off", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}

Listen.java

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.SystemRequirementsChecker;

import java.util.List;
import java.util.UUID;

public class Listen extends Activity {

    private BeaconManager beaconManager;
    private Region region;

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

        beaconManager = new BeaconManager(this);

        region = new Region("ranged region",
                UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null);
    }


    @Override
    protected void onResume() {
        super.onResume();

        SystemRequirementsChecker.checkWithDefaultDialogs(this);

        beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
            @Override
            public void onEnteredRegion(Region region, List<Beacon> list) {
                showNotification(
                        "Welcome..",
                        "to the AccessLab!");
            }
            @Override
            public void onExitedRegion(Region region) {
                showNotification(
                        "You've left the lab..",
                        "Goodbye!");
            }
        });


        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                beaconManager.startMonitoring(region);
            }
        });
    }



    public void showNotification(String title, String message) {
        Intent notifyIntent = new Intent(this, MainActivity.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
                new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();
        notification.defaults |= Notification.DEFAULT_SOUND;
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }

}

的AndroidManifest.xml

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

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <Activity android:name=".Listen"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.DEFAULT" />
            <category android:name="accesscontrol2016.Listen" />
        </intent-filter>
    </Activity>
</application>

正如您可以看到我已将Listen类添加到Android Manifest中的活动中。我不确定我是否正确设置了我的意图getApplicationContext()。任何帮助非常感谢。

2 个答案:

答案 0 :(得分:1)

解决起来很简单:

ActivityNotFoundException:...您是否在AndroidManifest.xml中声明了此活动?

好吧,好吗?

只需将其添加到您的清单中:

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

或者,如果您需要为活动

设置更多属性
    <activity
        android:name=".Activity"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.DEFAULT" />

            <category android:name="com.your.package.CLASS" />
        </intent-filter>
    </activity>

必须声明所有活动才能正常工作。以上是一个易于编辑以满足您需求的通用示例。请记住:在清单

中声明所有类似的活动

修改

活动代码无法大写:

<activity android:name=".Listen"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.DEFAULT" />
        <category android:name="accesscontrol2016.Listen" />
    </intent-filter>
</activity>

答案 1 :(得分:0)

在您发布的清单中,您有:

<Activity android:name=".Listen"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.DEFAULT" />
        <category android:name="accesscontrol2016.Listen" />
    </intent-filter>
</Activity>

首先,您不需要<intent-filter> <activity>声明中的Listen声明。而你需要使用更低级别的&#34; a&#34;在<activity>声明中。你使用了大写字母&#34; A&#34;。标签“#”无法识别(您的IDE应该告诉您)。