Android minSdkVersion并使用更高级别SDK中引入的类

时间:2010-07-18 04:21:25

标签: android

我正在测试android minSDKVersion属性并发现一个奇怪的行为 -

我在androidManifest.xml文件中输入了minSDKVersion = 3(1.5)和targetSDKVersion = 4(1.6)。

为了进行测试,我在onCreate方法的启动活动中添加了以下几行 -

 android.telephony.SmsManager sm = android.telephony.SmsManager.getDefault();
 ArrayList<String> stringArray = sm.divideMessage("this is message");
 Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show(); 

android.telephony.SmsManager类正在android 1.6中引入。

在添加这些行之后,在1.6模拟器上它显示了吐司,但在1.5模拟器上它没有显示吐司并且也没有崩溃。

我原以为应用程序会在1.5模拟器上崩溃,但这并没有发生。 谁能解释一下呢?

由于

1 个答案:

答案 0 :(得分:1)

尝试调用不可用的类的方法将导致异常。

我不清楚为什么你没有观察到崩溃。也许你有其他代码可以拯救你。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.versions"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MinSdk"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

活动代码:

package com.example.versions;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MinSdk extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SmsManager sm = SmsManager.getDefault();
        ArrayList<String> stringArray = sm.divideMessage("this is message");
        Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show(); 
    }
}

在Android 1.5模拟器中运行时转储Logcat:

E/dalvikvm(  779): Could not find method android.telephony.SmsManager.getDefault, referenced from method com.example.versions.MinSdk.onCreate
W/dalvikvm(  779): VFY: unable to resolve static method 3: Landroid/telephony/SmsManager;.getDefault ()Landroid/telephony/SmsManager;
W/dalvikvm(  779): VFY:  rejecting opcode 0x71 at 0x0008
W/dalvikvm(  779): VFY:  rejected Lcom/example/versions/MinSdk;.onCreate (Landroid/os/Bundle;)V
W/dalvikvm(  779): Verifier rejected class Lcom/example/versions/MinSdk;
W/dalvikvm(  779): Class init failed in newInstance call (Lcom/example/versions/MinSdk;)
D/AndroidRuntime(  779): Shutting down VM
W/dalvikvm(  779): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
E/AndroidRuntime(  779): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  779): java.lang.VerifyError: com.example.versions.MinSdk
E/AndroidRuntime(  779):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  779):    at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime(  779):    at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime(  779):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
E/AndroidRuntime(  779):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime(  779):    at android.app.ActivityThread.access$1800(ActivityThread.java:112)
E/AndroidRuntime(  779):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
E/AndroidRuntime(  779):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  779):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  779):    at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime(  779):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  779):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  779):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime(  779):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime(  779):    at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(  541): GC freed 2 objects / 48 bytes in 194ms
I/Process (  575): Sending signal. PID: 779 SIG: 3
I/dalvikvm(  779): threadid=7: reacting to signal 3
I/dalvikvm(  779): Wrote stack trace to '/data/anr/traces.txt'