简体   繁体   中英

Public Class InstantiationException

I made a class and its constructor should take 2 arguments, but Eclipse gives me an InstantiationException and I don't know why. How can I fix this?

Here is the Sms class code:

    public class Sms  {
        String message;
String phonenumber = "";
SharedPreferences  sp;
Context context;

        public Sms(String m, Context context)
        {

            message = m;
            this.context = context;
        } 

     public void sendSms()
        {
            sp  = PreferenceManager.getDefaultSharedPreferences(context);
               phonenumber = sp.getString("PHONE", "");


            SmsManager manager = SmsManager.getDefault();

            PendingIntent piSend = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
            PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);



                    int length = message.length();

                    if(length > MAX_SMS_MESSAGE_LENGTH)
                    {
                            ArrayList<String> messagelist = manager.divideMessage(message);

                            manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
                    }
                    else
                    {
                            manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
                    }

        }
    }

--------------------------------------------

And the LogCat:

02-16 17:20:26.579: E/AndroidRuntime(474): FATAL EXCEPTION: main
02-16 17:20:26.579: E/AndroidRuntime(474): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mioc.programing.safetpin/mioc.programing.safetpin.Sms}: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.os.Looper.loop(Looper.java:123)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-16 17:20:26.579: E/AndroidRuntime(474):  at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474):  at java.lang.reflect.Method.invoke(Method.java:507)
02-16 17:20:26.579: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-16 17:20:26.579: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-16 17:20:26.579: E/AndroidRuntime(474):  at dalvik.system.NativeStart.main(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): Caused by: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474):  at java.lang.Class.newInstanceImpl(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474):  at java.lang.Class.newInstance(Class.java:1409)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-16 17:20:26.579: E/AndroidRuntime(474):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-16 17:20:26.579: E/AndroidRuntime(474):  ... 11 more

And this is how I create the object from my Activity :

Intent open_SM = new Intent ("android.intent.action.SMS");
            startActivity(open_SM);
    Sms s = new Sms("trlababalan",this);
            s.sendSms()

;

here is manifest

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

      <uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  <uses-permission 
android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
  <uses-permission 
android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="mioc.programing.safetpin.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>

            <service android:name="TestService">
            </service>
             <activity
            android:name="mioc.programing.safetpin.Sms"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SMS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

You have declared Sms as an Activity in your Manifest:

<activity
    android:name="mioc.programing.safetpin.Sms"
    android:label="@string/app_name" >

But Sms doesn't extend Activity... So these lines cause the exception:

Intent open_SM = new Intent ("android.intent.action.SMS");
startActivity(open_SM);

If you want to run Sms as an Activity you need to turn the entire class into an Android Activity, or if you want Sms to be a utility don't use an Intent to start Sms as an Activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM