1

我试图找出一些我在堆栈溢出时发现的代码,并且真的很难让它工作,所以我转向这里的天才寻求帮助。希望我已经输入了足够的信息。

代码不会返回错误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();
            }

        }
    }
  }

这是GPS类的代码

package com.simon.startAppService2;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.TextView;
import android.widget.Toast;

public class GPS extends Activity implements LocationListener{
    TextView latitude,logitude;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

        Toast.makeText(this, "i m started", Toast.LENGTH_LONG);

        latitude = (TextView) findViewById(R.id.txtLat);
        logitude = (TextView) findViewById(R.id.txtLongi);
        latitude.setText("Loading...");
        logitude.setText("Loading...");


    }


    String LATTITUDE;
    String LOGITUDE;

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        double lat = location.getLatitude();
        double lag = location.getLongitude();

         LATTITUDE = Double.toString(lat);      
         LOGITUDE = Double.toString(lag);

         latitude.setText(LATTITUDE);
         logitude.setText(LOGITUDE);
         SmsManager sm = SmsManager.getDefault();
//      // here is where the destination of the text should go
         String number = "5554";
         sm.sendTextMessage(number, null, "latitude="+latitude.getText()+"\nlongitude="+logitude.getText(), null, null);


    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }


//  /** Register for the updates when Activity is in foreground */
//  @Override
//  protected void onResume()
//  {
//      super.onResume();
//      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
//  }
//
//  /** Stop the updates when Activity is paused */
//  @Override
//  protected void onPause() {
//      super.onPause();
//      lm.removeUpdates(this);
//  }

}

这是我的 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为另一个用户找到的代码,它看起来正在做我正在寻找的事情,因此任何人都可以提供的任何帮助/指导将不胜感激。

非常感谢

西蒙

4

2 回答 2

1

是的,txtLattxtLongiEditText如何被 as 找到的TextView

因此,选项 1 您可以将它们更改EditTextTextView在您的main.xml布局中。

<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" />

否则,您可以将这些值标识为EditText并将它们保存EditText在您的main.xml文件中。

EditText latitude,logitude;

latitude = (EditText) findViewById(R.id.txtLat);
logitude = (EditText) findViewById(R.id.txtLongi);
于 2012-08-31T06:24:57.623 回答
0

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

于 2012-08-31T06:30:23.247 回答