私は問題があります。ダイアログの SeekBar で画面の明るさを管理するための Brightness というクラスを作成しました。これがクラスです。
public class Brightness extends Activity{
private SeekBar brightbar;
private int brightness;
@SuppressWarnings("unused")
private ContentResolver cResolver;
private Window window;
TextView txtPerc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShowDialog();
}
public void ShowDialog() {
final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View Viewlayout = inflater.inflate(R.layout.dialog_brightness, (ViewGroup) findViewById(R.id.layout_dialog));
popDialog.setTitle("Set brightness");
popDialog.setView(Viewlayout);
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
cResolver = getContentResolver();
window = getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
cResolver = getContentResolver();
window = getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
try
{
brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc +" %");
}
catch (SettingNotFoundException e)
{
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
brightbar.setProgress(brightness);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
brightness = progress;
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc +" %");
}
});
popDialog.setNeutralButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
popDialog.create();
popDialog.show();
}
}
別のクラスである PreferenceActivity には、「明るさ」という設定があります。これをクリックすると設定ダイアログが開き、このコードを配置したいと思います。
Preference brigh;
brigh = (Preference) this.findPreference("brightness_pref");
brigh.setOnPreferenceClickListener( new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Brightness brightness = new Brightness();
brightness.ShowDialog();
return true;
}
});
しかし、アプリケーションを実行して環境設定をクリックすると、クラッシュします。これはログキャットです
11-10 18:23:34.955: E/AndroidRuntime(945): 致命的な例外: メイン 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.NullPointerException 11-10 18:23:34.955: E/AndroidRuntime(945): android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:146) 11-10 18:23:34.955: E/AndroidRuntime(945): android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java) :103) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.AlertDialog$Builder.(AlertDialog.java:360) 11-10 18:23:34.955: E/AndroidRuntime(945): com.simonedev.androtools.Brightness.ShowDialog(Brightness.java:39) 11 -10 18:23:34.955: E/AndroidRuntime(945): com.simonedev.androtools.Impostazioni$1.onPreferenceClick(Settings.java:48) 11-10 18:23:34.955: E/AndroidRuntime(945): android.preference.Preference.performClick(Preference.java:952) 11-10 18:23:34.955: E/AndroidRuntime(945) : android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215) 11-10 18:23:34.955: E/AndroidRuntime(945): android.widget.AdapterView.performItemClick(AdapterView.java:297) 11-10 18:23:34.955: E/AndroidRuntime(945): android.widget.AbsListView.performItemClick(AbsListView.java:1100) 11-10 18:23:34.955: E/AndroidRuntime(945): android.widget.AbsListView で$PerformClick.run(AbsListView.java:2788) 11-10 18:23:34.955: E/AndroidRuntime(945): android.widget.AbsListView$1.run(AbsListView.java:3463) 11-10 18:23: 34.955: E/AndroidRuntime(945): android.os.Handler.handleCallback(Handler.java:730) 11-10 18:23:34.955:E/AndroidRuntime(945): android.os.Handler.dispatchMessage(Handler.java:92) 11-10 18:23:34.955: E/AndroidRuntime(945): android.os.Looper.loop(Looper.java) :137) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.ActivityThread.main(ActivityThread.java:5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブ メソッド) でHandler.dispatchMessage(Handler.java:92) 11-10 18:23:34.955: E/AndroidRuntime(945): android.os.Looper.loop(Looper.java:137) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.ActivityThread.main(ActivityThread.java:5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(Nativeメソッド) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945) : com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main で(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブ メソッド) でHandler.dispatchMessage(Handler.java:92) 11-10 18:23:34.955: E/AndroidRuntime(945): android.os.Looper.loop(Looper.java:137) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.ActivityThread.main(ActivityThread.java:5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(Nativeメソッド) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945) : com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main で(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブ メソッド) でE/AndroidRuntime(945): android.os.Looper.loop(Looper.java:137) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.ActivityThread.main(ActivityThread.java) :5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 11-10 18:23:34.955: E/AndroidRuntime(945): at java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java で:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime (945): dalvik.system.NativeStart.main(ネイティブメソッド)E/AndroidRuntime(945): android.os.Looper.loop(Looper.java:137) 11-10 18:23:34.955: E/AndroidRuntime(945): android.app.ActivityThread.main(ActivityThread.java) :5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 11-10 18:23:34.955: E/AndroidRuntime(945): at java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java で:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime (945): dalvik.system.NativeStart.main(ネイティブメソッド)ActivityThread.main(ActivityThread.java:5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 11-10 18:23:34.955: E /AndroidRuntime(945): java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$ でMethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18: 23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブメソッド)ActivityThread.main(ActivityThread.java:5289) 11-10 18:23:34.955: E/AndroidRuntime(945): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 11-10 18:23:34.955: E /AndroidRuntime(945): java.lang.reflect.Method.invoke(Method.java:525) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$ でMethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18: 23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブメソッド)E/AndroidRuntime(945): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal .os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブ メソッド) でE/AndroidRuntime(945): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 11-10 18:23:34.955: E/AndroidRuntime(945): com.android.internal .os.ZygoteInit.main(ZygoteInit.java:555) 11-10 18:23:34.955: E/AndroidRuntime(945): dalvik.system.NativeStart.main(ネイティブ メソッド) で
解き方がわかりません。よろしくお願いします。