gpt4 book ai didi

java - 背景变化导致强制关闭

转载 作者:行者123 更新时间:2023-12-01 18:52:35 25 4
gpt4 key购买 nike

我刚刚编写了一些代码,如果选中了首选项中的复选框,则将我的背景更改为可绘制对象,如果没有选中,则将其变为白色。该代码在我的 MainActivity 中工作正常,但在另一个 Activity (LinksActivity) 中给出了 NullPointerException。有人可以纠正我吗?

package nl.plplpl.ccs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.LinearLayout;


public class LinksActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
LinearLayout linkslayout = (LinearLayout) findViewById(R.id.linkslayout);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
Boolean bg = prefs.getBoolean("background", false);
if (bg){

if (Build.VERSION.SDK_INT >= 16)
linkslayout.setBackground(getResources().getDrawable(R.drawable.background));
else
linkslayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
} else {
linkslayout.setBackgroundResource(R.color.white);
}
super.onResume();
}
}

最佳答案

您忘记设置 Activity 内容。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
}

关于java - 背景变化导致强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com