小编Che*_*eng的帖子

android.widget.TextView.checkForRelayout期间的NullPointerException

以前,在项目目标KitKat中,我有一个相当简单ArrayAdapter的使用Spinner没有任何麻烦.

CountryArrayAdapter.java

public class CountryArrayAdapter extends ArrayAdapter<Country> {
    private static class ViewHolder0 {
        public TextView textView0;
    }

    private static class ViewHolder1 {
        public CheckedTextView checkedTextView0;
    }

    private static List<Country> getValidCountries() {
        List<Country> countries = new ArrayList<Country>(Arrays.asList(Country.values()));
        return Collections.unmodifiableList(countries);
    }

    public CountryArrayAdapter(Context context) {
        super(context, R.layout.country_spinner_item, validCountries);
        this.setDropDownViewResource(R.layout.country_spinner_dropdown_item);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.country_spinner_dropdown_item, null); …
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
1
解决办法
794
查看次数

当应用程序在前台时避免通知弹出窗口

https://material.io/guidelines/patterns/notifications.html#notifications-behavior,我真的能够通知用户,而无需显示通知。

我想在状态栏中显示一个闪烁的喜欢图标,而不会弹出通知查看。(如果您观看 From https://material.io/guidelines/patterns/notifications.html#notifications-behavior部分下的第一个视频,您可以看到状态栏中的闪烁)

状态栏中闪烁的图标

在此处输入图片说明

但是,我不完全确定如何实现这一目标。每当我通知用户时,都会有一个通知弹出窗口。

通知弹出窗口

在此处输入图片说明

我的代码如下

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context.getApplicationContext(), org.yccheok.notification.Utils.createNotificationChannel())
    .setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle(contentTitle)
    .setTicker(ticker)
    .setColorized(true)
    .setColor(context.getResources().getColor(R.color.accent_material_light))
    .setContentText(contentText);

mBuilder.setSound(Uri.parse(getStockAlertSound()));

mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

mBuilder.setAutoCancel(true);

// Need BIG view?
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle(contentTitle);
inboxStyle.setSummaryText(summaryText);
for (SpannableString notificationMessage : notificationMessages) {
    inboxStyle.addLine(notificationMessage);
}
mBuilder.setStyle(inboxStyle);

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

我想知道,当我的应用程序处于前台时,如何避免通知弹出窗口偷看,但只在状态栏中显示一个闪烁的图标。

android

4
推荐指数
1
解决办法
807
查看次数

关于“升级到 Room 2.2.0 时处理列默认值”的困惑?

我指的是https://developer.android.com/training/data-storage/room/migrating-db-versions.md#handle-default-values-migrations

我对指南感到困惑。我的理解是

如果添加NON NULL新列

@Entity
public class Song {
    // ...
    @NonNull
    final String tag;
}
Run Code Online (Sandbox Code Playgroud)

使用以下ALTER TABLE迁移策略是错误的


错误的

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL(
            "ALTER TABLE Song ADD COLUMN tag TEXT NOT NULL DEFAULT ''");
    }
};
Run Code Online (Sandbox Code Playgroud)

您需要使用以下drop and re-create迁移策略

正确的

static final Migration MIGRATION_2_3 = new Migration(2, 3) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("CREATE TABLE new_Song (" +
                "id …
Run Code Online (Sandbox Code Playgroud)

android android-room

4
推荐指数
1
解决办法
1797
查看次数

如果我想从 Codable 中排除某些属性,为什么这些属性必须是可选的?

我有以下内容struct

struct Checklist : Codable {
    let id: Int64
    var text: String?
    var checked: Bool
    var visible: Bool
    var version: Int64

    private enum CodingKeys: String, CodingKey {
        case id
        case text
        case checked
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到编译器错误

类型“清单”不符合“可解码”协议

我可以解决的唯一方法是将排除的属性更改为可选。

struct Checklist : Codable {
    let id: Int64
    var text: String?
    var checked: Bool
    var visible: Bool?
    var version: Int64?

    private enum CodingKeys: String, CodingKey {
        case id
        case text
        case checked
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以知道为什么会这样吗?这是解决此类编译器错误的唯一正确方法吗?

swift codable

4
推荐指数
1
解决办法
505
查看次数

Firebase Crashlytics 何时将 Proguard/R8 映射文件上传到他们的服务器?

一直以来,我们都在使用 Google Play Console 来捕获崩溃报告。

我们需要手动上传 Proguard/R8 映射文件,以对崩溃堆栈跟踪进行反混淆。

在此处输入图片说明

根据https://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?authuser=0&platform=android/sf/answers/3310470271/,不再需要此类操作。

我可以知道,幕后发生了什么吗?Firebase Crashlytics 何时将 Proguard/R8 映射文件上传到他们的服务器?

android firebase crashlytics

4
推荐指数
1
解决办法
2152
查看次数

Room 2.1.0之后升级的defaultValue迁移要求的混乱

在Room 2.1.0中,常见有以下代码

版本2

@Entity(tableName = "password")
public class Password {
    @ColumnInfo(name = "dummy0")
    @NonNull
    public String dummy0;

}

public class Migration_1_2 extends Migration {
    public Migration_1_2() {
        super(1, 2);
    }

    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE password ADD COLUMN dummy0 TEXT NOT NULL DEFAULT ''");
    }
}
Run Code Online (Sandbox Code Playgroud)

迁移指南来自

很混乱。

注意:如果您的数据库架构已经具有默认值,例如通过 ALTER TABLE x ADD COLUMN y INTEGER NOTNULL DEFAULT z 添加的默认值,并且您决定通过 @ColumnInfo 将默认值定义到相同的列,那么您可能需要提供迁移验证未考虑的默认值。有关详细信息,请参阅房间迁移。


在升级到2.2.3之前,有2种可能

  1. 如果迁移运行,我们有一dummy0列具有默认值。
  2. 或者,dummy0如果这是新数据库,我们有一列没有默认值。

当我们升级到 Room 2.1.0 到 Room …

android android-room androidx

4
推荐指数
1
解决办法
3797
查看次数

为什么 Int 枚举作为字典键,产生与 Int 作为字典键不同的 json 字符串?

我尝试使用 Int 枚举转换字典

enum TypeE: Int, Codable
{
    case note = 1
    case tab
}

let encoder = JSONEncoder()

let dictionary0 = [TypeE.note:"VALUE0", TypeE.tab:"VALUE1"]
var data = try encoder.encode(dictionary0)
var string = String(data: data, encoding: .utf8)!
// [1,"VALUE0",2,"VALUE1"]
print(string)
Run Code Online (Sandbox Code Playgroud)

生成的json字符串输出是

[1,"VALUE0",2,"VALUE1"]
Run Code Online (Sandbox Code Playgroud)

对我来说看起来很奇怪。因为,生成的 json 字符串表示一个数组。


如果我测试

let encoder = JSONEncoder()

let dictionary1 = [1:"VALUE0", 2:"VALUE1"]
var data = try encoder.encode(dictionary1)
var string = String(data: data, encoding: .utf8)!
// {"1":"VALUE0","2":"VALUE1"}
print(string)
Run Code Online (Sandbox Code Playgroud)

生成的json字符串输出是

{"1":"VALUE0","2":"VALUE1"}
Run Code Online (Sandbox Code Playgroud)

似乎如果我使用 Int 枚举作为字典键,生成的 json 字符串将成为数组的表示?

我的代码中是否有任何错误,或者我的期望不正确?

json ios swift

4
推荐指数
1
解决办法
71
查看次数

如何正确设置垂直堆栈视图中项目高度的动画?

我有红色堆栈视图。

它包含一个标签和一个紫色自定义视图。

看起来如下

在此输入图像描述

我想实现的是

  1. 动画期间,标签的左上角 (x, y) 将始终保持不变。
  2. 紫色自定义视图的左上角 (x, y) 在动画过程中将始终保持不变。
  3. 在动画过程中,红色堆栈视图将基于紫色自定义视图的动画优雅地进行动画处理。

我使用下面的代码

class ViewController: UIViewController {

    @IBOutlet weak var purpleView: UIView!
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var purpleViewHeightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    var value: CGFloat = 200.0
    @IBAction func buttonClicked(_ sender: Any) {
        if value == 500 {
            value = 200.0
        } else {
            value = 500.0
        }
        
        UIView.animate(withDuration: 2) {
            self.purpleViewHeightConstraint.constant = self.value …
Run Code Online (Sandbox Code Playgroud)

ios swift

4
推荐指数
1
解决办法
819
查看次数

Perl中的进程间互斥量

我有一个在mod_perl下执行的Perl CGI程序。在该程序中,我想防止资源同时被多个进程访问。

# Semaphore Initialization Code
# 10023 is unique id, and this id will be same across different apache process.
# 1, Only one semaphore being created.
# 0722, as all process will be execute under apache account. Hence, they will all having '7' privilege.
my $sem = new IPC::Semaphore(10023, 1, 0722 | IPC_CREAT); # Code(1)
# Set 0th (one and only one) semaphore's value to 1, As I want to use this semaphore as mutex.
$sem->setval(0, 1);                                       # …
Run Code Online (Sandbox Code Playgroud)

perl semaphore ipc

3
推荐指数
1
解决办法
4026
查看次数

锁定自由数组元素交换

在多线程环境中,为了进行线程安全的数组元素交换,我们将执行同步锁定.

// a is char array.
synchronized(a) {
    char tmp = a[1];
    a[1] = a[0];
    a[0] = tmp;
}
Run Code Online (Sandbox Code Playgroud)

在上述情况下我们是否可以使用以下API,以便我们可以进行无锁数组元素交换?如果有,怎么样?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29

java concurrency multithreading deadlock locking

3
推荐指数
1
解决办法
2333
查看次数