开发者

Unable to instantiate error?

Here is my source code.

public class MainActivity extends Activity {
  private static String content;
  private static String phone;
  private String number;
  private String message;

  private  BroadcastReceiver receiver =  new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            number = "";
             message = "";
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.lengt开发者_如何学编程h];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                number = msgs[i].getOriginatingAddress();                     

               message = msgs[i].getMessageBody();


            }
            //---display the new SMS message--- 
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            SendMe();
    }
    }


};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter(); 
     registerReceiver(receiver, filter);
    setContentView(R.layout.main);



    }


             public void SendMe(){


    PendingIntent pi =  PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);  
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, message, pi, null);

}
}

I keep getting this error in the logcat

06-28 17:11:23.241: ERROR/AndroidRuntime(1311): java.lang.RuntimeException: Unable to instantiate receiver com.ftt.autospond.MainActivity: java.lang.ClassCastException: com.ftt.autospond.MainActivity

Here is my manifest

 <?xml version="1.0" encoding="utf-8"?>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <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>
           <receiver android:name="com.ftt.autospond.MainActivity"> 
        <intent-filter> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 


Make sure that your activities are registered in your androidmanefest.xml file

edit: you can't have your receiver be registered in a preexisting class like that in your manifest. If you do it dynamically in your class there is no need to have it in your manifest... pull that receiver out and see if that takes care of it


register the receiver after setContentView(R.layout.main);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜