android.app.Application无法强制转换为MainActivity

时间:2017-05-18 16:22:50

标签: java android android-studio

当我试图从我的衣服到我的手机开始一个意图时,我收到了这个错误。当我按下手表上的按钮时,会向收听服务员发送一条消息。我尝试从我的listenerService启动一个方法到MainActivity,但我收到了错误:

  

java.lang.ClassCastException:android.app.Application无法强制转换为com.my_emergency.samdesmedt.my_emergency.MainActivity
  在com.my_emergency.samdesmedt.my_emergency.ListenerServiceFromWear.onMessageReceived(ListenerServiceFromWear.java:28)

这是我的清单:

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

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AddActivity">
        </activity>
        <activity
            android:name=".SettingsActivity">
        </activity>
        <activity
            android:name=".EditSettingsActivity">
        </activity>
        <activity
            android:name=".EditActivity" >
        </activity>

        <service android:name=".ListenerServiceFromWear">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
            </intent-filter>
        </service>
    </application>

</manifest>

这是我的ListenerServiceFromWear.Java

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);

    if (messageEvent.getPath().equals(WEARPATH)) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("isEmergency", true);
        startActivity(intent);
    }
}

这是我的mainActivity中的代码

Intent intent = getIntent();
boolean isEmergency = intent.getBooleanExtra("isEmergency", false);

if(isEmergency){
    startEmergencyMode(message);
}

1 个答案:

答案 0 :(得分:1)

Intent intent = new Intent(getBaseContext(), MainActivity.class);

修好了