ClassCastException,无法在Android上实例化活动

ClassCastException,无法在Android上实例化活动,android,android-activity,broadcastreceiver,classcastexception,Android,Android Activity,Broadcastreceiver,Classcastexception,我正试图找出一些我在堆栈溢出中找到的代码,并且真的很难让它工作,所以我向这里的天才们寻求帮助。希望我能输入足够的信息 代码不会在eclipse中返回错误,但是当我部署到模拟器时,会在LogCat中出现以下错误,导致应用程序崩溃 Unable to instantiate activity ComponentInfo{com.simon.startAppService2/com.simon.startAppService2.StartAppService2Activity}:java.lang.C

我正试图找出一些我在堆栈溢出中找到的代码,并且真的很难让它工作,所以我向这里的天才们寻求帮助。希望我能输入足够的信息

代码不会在eclipse中返回错误,但是当我部署到模拟器时,会在LogCat中出现以下错误,导致应用程序崩溃

Unable to instantiate activity ComponentInfo{com.simon.startAppService2/com.simon.startAppService2.StartAppService2Activity}:java.lang.ClassCastException:com.simon.startAppService2.StartAppService2Activitycannot be cast to android.app.Activity
以下是StartAppService2Activity的代码

package com.simon.startAppService2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class StartAppService2Activity extends BroadcastReceiver
{
    public static String trigger_message="";
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                trigger_message=msgs[i].getMessageBody().toString();
                str += trigger_message;
                str += "\n";
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            if(trigger_message.equals("dx"))
            {
                Toast.makeText(context, "I am triggered",Toast.LENGTH_LONG).show();
                ///////////////////////////
                //i want to start here
                //////////////////////////
                //MainScreenActivity.trigger="Now";
                //               Intent i = new Intent(context,GPS.class); 
                //                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                //                  context.startActivity(i); 
                    //start activity
                Intent i = new Intent();
                i.setClassName("com.simon.startAppService2", "GPS.class");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(i);
            }
            else
            {
                Toast.makeText(context, "I am not triggered,  Bbyz!!!",Toast.LENGTH_LONG).show();
            }

        }
    }
  }
这是我的AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>  

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".StartAppService2Activity"
            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=".GPS"/>
    </application>

</manifest>

这是我的main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <EditText
        android:id="@+id/txtLat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

    </EditText>

    <EditText
        android:id="@+id/txtLongi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

</LinearLayout>

正如我所提到的,这是我在
StackOverflow
上为另一个用户找到的代码,该用户似乎正在做我正在寻找的事情,因此任何人能够提供的任何帮助/指导都将不胜感激

非常感谢


Simon

是的,
txtLat
txtLongi
EditText
如何通过as
TextView找到它的

因此,选项1可以将
main.xml
布局中的
EditText
更改为
TextView

<TextView
    android:id="@+id/txtLat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<TextView
    android:id="@+id/txtLongi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

是的,
txtLat
txtLongi
EditText
如何通过as
TextView找到它

因此,选项1可以将
main.xml
布局中的
EditText
更改为
TextView

<TextView
    android:id="@+id/txtLat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<TextView
    android:id="@+id/txtLongi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

这可能是因为Intent中的上下文是
StartAppService2Activity
,它没有扩展
android.app.Activity

,这可能是因为Intent中的上下文是
StartAppService2Activity
,它没有扩展
android.app.Activity

,你说得对。即使这样也会出现同样的异常,但是通过查看日志cat,异常发生在StartAppService2ActivityTanks中,当我看到textview时,我快速尝试了一下,但没有成功。谢谢您的建议。@SpK,是的,我将main.xml改为引用TextView而不是EditText。不幸的是,我也犯了同样的错误。谢谢你的建议,你说得对。即使这样也会出现同样的异常,但是通过查看日志cat,异常发生在StartAppService2ActivityTanks中,当我看到textview时,我快速尝试了一下,但没有成功。谢谢您的建议。@SpK,是的,我将main.xml改为引用TextView而不是EditText。不幸的是,我也犯了同样的错误。谢谢你的建议。谢谢阿奇,所以我的StartAppService2活动应该扩展活动,而不是BroadcastReceiver?谢谢阿奇,我想这会让我的onReceive意图出错。干杯谢谢阿尔奇,所以我的StartAppService2活动应该扩展活动,而不是广播接收器?谢谢阿尔奇,我想这会让我的onReceive意图出错。干杯
EditText latitude,logitude;

latitude = (EditText) findViewById(R.id.txtLat);
logitude = (EditText) findViewById(R.id.txtLongi);