Androidアプリの背景色を変更しようとしています(ADKは初めてです)。
別の質問で、メインレイアウト(RelativeLayout)とアプリ内の他のすべてのビューの間で別のLinearLayoutを使用し、代わりにその色を変更する必要があることを読みました。背景をどの色に変更するかを設定して、設定アクティビティとすべてがスムーズに実行されるようにします。ただし、R.id.bg(bgはLinearLayoutのID)をfindViewById()に渡すと、nullが返され、背景色を変更しようとするたびにNPEがスローされます。
編集:Y'知っている、これがクラス全体のソースです。:P
public class Preferences extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
private void showToast(CharSequence text) {
Context context = getApplicationContext();
showToast(context, text, 3000);
}
private void showToast(Context context, CharSequence text, int duration) {
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equalsIgnoreCase("list_color")) {
LinearLayout bg = (LinearLayout) findViewById(R.id.bg);
String color = sharedPreferences.getString("list_color", "White");
if (bg == null) {
showToast("Unable to change background color");
} else {
if (color.equals("White")) {
bg.setBackgroundColor(android.R.color.white);
showToast("Background color set to white");
} else if (color.equals("Black")) {
bg.setBackgroundColor(android.R.color.black);
showToast("Background color set to black");
}
} // end NP test
}
}
}