首页 > 代码库 > Android studio

Android studio

 


情况一: 
描述:写Android app时,出现activity对应的layout预览图所显示的activity是有标题栏的,但是运行该app,无论是模拟器还是真机,却出现没有了标题栏的情况,而自己没有特地隐藏标题栏。 
解决:此时出现标题栏消失可能是因为activity继承是Activity而不是AppCompatActivity,而我们在创建app之初,AS默认是继承AppCompatActivity,AndroidManifest.xml文件里对应的theme则是

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

 


情况二: 
描述:多个Fargment使用同一layout ID,在进行来回切换操作时出现“Java.lang.IllegalStateException: commit already called”的错误。 
解决:由于本人将FragmentTransaction变量定义为全局变量,导致只能commit一次,所以只要将FragmentTransaction变量定义为局部变量即可。


情况三: 
描述:使用FragmentTransaction的replace方法时出现错误:

‘replace(int, android.app.Fragment, java.lang.String)‘ in ‘android.app.FragmentTransaction‘ cannot be applied to ‘(int, com.jarwell.learnactionbar.ContentFragment, java.lang.String)‘

 

 

解决:此种情况发生在ContentFragment所继承的Fragment是android.support.v4.app.Fragment,而FragmentTransaction对象所导入的包是android.app.FragmentTransaction,由此出现不兼容的情况。据此只要统一导入包的类型即可,譬如可以分别:import android.app.Fragment和import android.app.FragmentTransaction


情况四: 
描述:如何添加assets文件夹? 
解决 
首先,在“工程名.iml”文件里的configuration标签下添加以下代码:

<option name="ASSETS_FOLDER_RELATIVE_PATH" value=http://www.mamicode.com/"/src/main/assets"/>

 

 

然后,在main文件夹里新建一个子文件夹并命名为assets,到此完成,之后就可以往里面添加文件了。


情况五: 
描述:在JSON解析时出现了以下错误日志:

08-31 15:36:59.369 13195-13195/? E/dalvikvm: Could not find class ‘android.util.ArrayMap‘, referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
08-31 15:36:59.370 13195-13195/? E/dalvikvm: Could not find class ‘android.util.ArrayMap‘, referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
08-31 15:36:59.381 13195-13195/? E/dalvikvm: Could not find class ‘android.os.PersistableBundle‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.382 13195-13195/? E/dalvikvm: Could not find class ‘android.os.PersistableBundle‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.382 13195-13195/? E/dalvikvm: Could not find class ‘android.media.session.MediaController‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.383 13195-13195/? E/dalvikvm: Could not find class ‘android.widget.Toolbar‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.386 13195-13195/? E/dalvikvm: Could not find class ‘android.app.ActivityManager$TaskDescription‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.391 13195-13195/? E/dalvikvm: Could not find class ‘android.app.SharedElementCallback‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.393 13195-13195/? E/dalvikvm: Could not find class ‘android.os.PersistableBundle‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.395 13195-13195/? E/dalvikvm: Could not find class ‘android.transition.TransitionManager‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.402 13195-13195/? E/dalvikvm: Could not find class ‘android.app.SharedElementCallback‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.410 13195-13195/? E/dalvikvm: Could not find class ‘android.app.assist.AssistContent‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.412 13195-13195/? E/dalvikvm: Could not find class ‘android.view.SearchEvent‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.416 13195-13195/? E/dalvikvm: Could not find class ‘android.os.PersistableBundle‘, referenced from method com.jarwell.learnjson.MainActivity.access$super
08-31 15:36:59.477 13195-13195/? E/dalvikvm: Could not find class ‘android.graphics.drawable.RippleDrawable‘, referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
08-31 15:37:32.664 518-795/? E/FWKEXT: Enter GetCurrentOP

 

解决:出现以上情况,是因为代码所调用的一些方法只能在较高的API level(高于minSdkVersion)上使用,而调试用的机子或模拟器所用的SDK Version小于该API level。如日志第一行提示中的ArrayMap,通过查看Android官方API可得到如下信息:

Added in API level 19

 

而本人的project的build.gradle如下:

apply plugin: ‘com.android.application‘

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.jarwell.learnjson"
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
        }
    }
}

dependencies {
    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
    testCompile ‘junit:junit:4.12‘
    compile ‘com.android.support:appcompat-v7:24.2.0‘
}

 

其中所使用的minSdkVersion17targetSdkVersion 为24,而run app的机子对应的API level恰好是17(17<19<24),因此出现了以上的错误日志信息,尝试用level为19以上的机子或模拟器则运行正常。

Android studio