0

我创建了一个类,它的构造函数应该有 2 个参数,但是 Eclipse 给了我一个InstantiationException,我不知道为什么。我怎样才能解决这个问题?

这是Sms课程代码:

    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);
                    }

        }
    }

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

和 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

这就是我从以下创建对象的方式Activity

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

;

这是明显的

<?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>
4

1 回答 1

0

您已在 Manifest 中声明Sms为 Activity:

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

Sms不扩展 Activity... 所以这些行会导致异常:

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

如果你想Sms作为一个 Activity 运行,你需要将整个类变成一个 Android Activity,或者如果你想Sms成为一个实用程序,不要使用 IntentSms作为 Activity 启动。

于 2013-02-16T16:47:47.957 回答