如何設置背景顏色從對話框(SettingsFColorDialod)到活動?有一個片段活動,其中一個片段的按鈕打開對話框,在sharedpreferences中有一些用於更改顏色的按鈕。它的工作原理,但只有當活動重新加載。Android設置顏色
我的對話框:
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import static com.hgyghyfghyu.apkana40.AppData.FCOLOR_KEY;
import static com.hgyghyfghyu.apkana40.AppData.TRANSP_KEY;
import static com.hgyghyfghyu.apkana40.AppData.editordata;
import static com.hgyghyfghyu.apkana40.AppData.prefdata;
// ...
public class SettingsFColorDialod extends DialogFragment {
public SettingsFColorDialod() {
}
public static SettingsFColorDialod newInstance(String title) {
SettingsFColorDialod frag = new SettingsFColorDialod();
Bundle args = new Bundle();
args.putString("Choose your color", title);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.settingsfcolordialog, container);
}
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
prefdata = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
editordata = prefdata.edit();
view.findViewById(R.id.FCOLOR1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editordata.putString(FCOLOR_KEY,"FF9999").apply();
changecolors();
dismiss();
}
});
String title = getArguments().getString("Choose your color", "Choose your color");
getDialog().setTitle(title);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
private void changecolors(){
prefdata = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
getView().findViewById(R.id.mainavatarbck).setBackgroundColor(Color.parseColor(prefdata.getString(TRANSP_KEY, "#CC") + prefdata.getString(FCOLOR_KEY, "33777F")));
getView().findViewById(R.id.mainnamebck).setBackgroundColor(Color.parseColor(prefdata.getString(TRANSP_KEY, "#CC") + prefdata.getString(FCOLOR_KEY, "33777F")));
}
}
問題是「changecolors」,我試圖創建一個不刷新此方法改變backgroundcolors但有錯誤(不幸的是應用程序已經停止),並從Android的顯示器:
FATAL EXCEPTION: main
Process: com.hgyghyfghyu.apkana40, PID: 22743
java.lang.NullPointerException
at com.hgyghyfghyu.apkana40.SettingsFColorDialod.changecolors(SettingsFColorDialod.java:176)
at com.hgyghyfghyu.apkana40.SettingsFColorDialod.access$000(SettingsFColorDialod.java:23)
at com.hgyghyfghyu.apkana40.SettingsFColorDialod$16.onClick(SettingsFColorDialod.java:159)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
在哪個文件中調用changecolor()? – Pooya
'mainavatarbck'和'mainnamebck'是Textview或LinearLayouts? –
@jankigadhiya mainavaterbck和mainnamebck是MainActivity中的LinearLayouts – littlewombat