这本书出版到现在已经过去了两年多,很多东西都过时了,学到现在为止也踩了很多坑,突然想到,还是记录一下比较好,从第六章开始,前面的看情况补充吧

1.补充

在Android Studio 3.2中,重写方法时添加的nullable,nonnull注解出现问题,经查是软件bug,解决方法是添加新版库androidx.annotation
implementation 'androidx.annotation:annotation:1.0.2'
参照https://blog.csdn.net/weixin_39306574/article/details/86716296

6.5 LitePal库

1. LitePal3.0开始不支持api14及以下(但是默认的工程创建是最低支持到api14)

现象:

Manifest merger failed : uses-sdk:minSdkVersion 14 cannot be smaller than version 15 declared in library [org.litepal.android:java:3.0.0] C:\……\AndroidManifest.xml as the library might be using APIs not available in 14
    Suggestion: use a compatible library with a minSdk of at most 14,
        or increase this project's minSdk version to at least 15,
        or use tools:overrideLibrary="org.litepal.java" to force usage (may lead to runtime failures)

解决方案:打开app/build.gradle,将minSdkVersion改到15及以上,如:

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.litepaltest"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

2. “6.5.4使用LitePal添加数据”,在Litepal3.0中,实体继承的DataSupport已经被完全废弃,LitepalSupport取而代之

代码修改为:

public class Book extends LitePalSupport {
    ……………
}

2. “6.5.6使用LitePal删除数据”以及之后的内容,因为DataSupport的废弃,静态方法通过LitePal类来调用

代码修改为:

Button deleteButton = (Button) findViewById(R.id.delete_data);
deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.deleteAll(Book.class, "price < ?", "15");
            }
        });
private static final String TAG = "MainActivity";//这句加在开头

Button queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> books = LitePal.findAll(Book.class);
                for (Book book : books) {
                    Log.d(TAG, "book name is " + book.getName());
                    Log.d(TAG, "book author is " + book.getAuthor());
                    Log.d(TAG, "book page is " + book.getPage());
                    Log.d(TAG, "book price is " + book.getPrice());
                    Log.d(TAG, "book press is " + book.getPress());
                }
            }
        });

类似的替换还有像findFirst()这一类的函数

Last modification:May 19th, 2019 at 04:47 pm