shared preferences : float - android

something is wrong with it
package com.sahandxx3.Countries;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
/**
* Created by Setude on 17-Dec-15.
*/
public class Setting extends Activity {
SharedPreferences sh;
TextView txtflag,txt_test;
SeekBar sbflag;
int progress = 10;
float size=10;
float txt_size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
init();
txtflag.setText("Flag size : "+sbflag.getProgress());
sbflag.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
txt_size=sh.getFloat("size",0);
progress= i;
size=(progress*5)+5;
txtflag.setText("Flag size : "+progress);
txt_test.setTextSize(size);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
txtflag.setText("Flag size : "+progress);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
txtflag.setText("Flag size : "+progress);
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
SharedPreferences.Editor edit=sh.edit();
edit.putFloat("size",size);
edit.commit();
}
});
}
private void init() {
txtflag= (TextView) findViewById(R.id.txtflag);
sbflag= (SeekBar) findViewById(R.id.sbflag);
txt_test= (TextView) findViewById(R.id.btn_test);
}
}
so my problem is with this line:
txt_size=sh.getFloat("size",0);
When I try to change the seekbar,It stops working.
then I want to use txt_size in txt_test();

My best guess is that your SharedPreferences instance sh is null at the moment you try to access it. Try to move sh=PreferenceManager.getDefaultSharedPreferences(Setting.this); to your onCreate().

Put below code on onCreate():
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
Your onCreate() method should looks like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
..
..
}
Hope this will help you.

I think you should use
float size=10f;
txt_size=sh.getFloat("size",0f);

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sh=PreferenceManager.getDefaultSharedPreferences(this);
}

Related

How Can I Update the ListView using "onProgressChanged" method of the SeekBar?

I have a simple layout have only
1 SeekBar "seekBar"
and
1 ListView "listView"
How Can I Update the ListView using "onProgressChanged" method of the SeekBar ?
What code should be used ?
package ahmed_smae.timestables;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Member Variables...
SeekBar seekBar;
ListView listView;
ArrayList<Integer> numbersArrayList = new ArrayList<Integer>();
onCreate method
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Build List
for(int i = 1; i <= 100; i++){
numbersArrayList.add(i);
}
Activate SeekBar and ListView
seekBar = (SeekBar) findViewById(R.id.seekBar);
listView = (ListView) findViewById(R.id.numbersListView);
link Adapter
final ArrayAdapter<Integer> numberListAdapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_list_item_1, numbersArrayList);
listView.setAdapter(numberListAdapter);
seekBar.setMin(numberListAdapter.getItem(0));
seekBar.setMax(numberListAdapter.getCount() + 1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("TimesTable", "" + i);
set code to update the ListView "listView" items with the SeekBar Progress Change
What Code Goes Here?
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Its very easy you can Refresh Adapter by using.
numberListAdapter.notifydatasetchanged();
Just put the numberArraysList inside onProgressChanged method of OnSeekBarChangeListener. Like this...
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
ArrayList<Integer> numbersArrayList = new ArrayList<Integer>();
Log.i("TimesTable", "" + i);

SharedPreferences in Non-Activity

Hi I need to get a String from an Activity to another Class. I need the String VideoID in the Test2 class. I wanted to use sharedpreferences, but this does not work. How can i use sharedpreferences here? Or any other idea how i can get the String? Thanks
In my Activity I have this Button:
// Video Button
final Button videobutton = (Button) findViewById(R.id.videobutton);
videobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Video ID save
SharedPreferences.Editor editor = getSharedPreferences("VideoID_saver", MODE_PRIVATE).edit();
editor.putString("VideoID", current_video);
editor.commit();
Intent OpenTest = new Intent(Questions.this, Test2.class);
startActivity(OpenTest);
}
});
The Test2.class looks like this:
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
public class Test2 extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyCb2pCHbqhBDKaYzFvU7g1SBG14YrM3XWE";
//Test
SharedPreferences prefs = getSharedPreferences("VideoID_saver", MODE_PRIVATE);
String testtext = prefs.getString("VideoID", "");
//http://youtu.be/<VIDEO_ID>
public final String VIDEO_ID = testtext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
/** Initializing YouTube player view **/
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {}
#Override
public void onPaused() {}
#Override
public void onPlaying() {}
#Override
public void onSeekTo(int arg0) {}
#Override
public void onStopped() {}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {}
#Override
public void onError(ErrorReason arg0) {}
#Override
public void onLoaded(String arg0) {}
#Override
public void onLoading() {}
#Override
public void onVideoEnded() {}
#Override
public void onVideoStarted() {}
};
You're probably somehow accessing different SharedPreferences instances. It's easier to make sure this doesn't happen using the getDefaultSharedPreferences() method:
In you Activity:
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putString("VideoID", current_video)
.apply();
In your Test2 onCreate method:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String testtext = prefs.getString("VideoID", "");
Couple of things. Test2 is inheriting an activity it seems, so not sure why your title says Non-Activity. SharedPreferences reading and writing seems ok but maybe reading the sharedpreferences even before the constructor on the activity has had change to run is too early.
Your goal is to get a string from button to test2. It seems like it would be better to avoid shared preferences entirely and pass the string to the next activity directly via the put/getExtra function.
Like so:
Intent OpenTest = new Intent(Questions.this, Test2.class);
i.putExtra("VideoID", current_video);
startActivity(OpenTest);
Then extract it from your new activity in your OnCreate method:
//http://youtu.be/<VIDEO_ID>
public final String VIDEO_ID;
#
Override
protected void onCreate(Bundle savedInstanceState) {
//Test
Intent intent = getIntent();
VIDEO_ID = intent.getExtras().getString("VideoID");
}

Android seekbar progress not update

I am using seekBar for soundpool volume control on my app, I use PopupWindow for seekbar and everything works fine except, whenever I change or pause my activity, and I open seekbar popupwindow again, the seekbar is no longer the value I last had saved. But it always returns to its default value 100%, even I use SharedPreferences what works well, but only if I do not leave current activity.
public class ActivityMain extends Activity implements OnTouchListener, OnMenuItemClickListener {
SoundManager snd;
OnSeekBarChangeListener barChange1;
private int soundID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_activity);
}
public void onRestart() {
super.onRestart();
}
public void onResume() {
super.onResume();
final Button btnOpenPopup = (Button)findViewById(R.id.button6);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
barChange1 = new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
snd.setVolume((float)progress/100.0f);
}
};
SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1);
volbar1.setMax(100);
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100);
volbar1.setProgress(value);
volbar1.setOnSeekBarChangeListener(barChange1);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 60, 20);
}});
{
}
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundManager(getApplicationContext());
soundID = snd.load(R.raw.sound_1);
ImageView img01 = (ImageView) findViewById(R.id.imageView11);
img01.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
snd.play(soundID);
return false;
}
});
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
#Override
protected void onPause() {
super.onPause();
snd.unloadAll();
}
}
Put this onResume()
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100)
volbar1.setProgress(value);
I am doing the same thing. Its working for me. Here is my code. It does not have all the Sound stuff.
package com.titlesource.testproject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;
public class ActivityMain extends AppCompatActivity {
SeekBar.OnSeekBarChangeListener barChange1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_main);
}
public void onRestart() {
super.onRestart();
}
public void onResume() {
super.onResume();
final Button btnOpenPopup = (Button)findViewById(R.id.button6);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
barChange1 = new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
};
SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1);
volbar1.setMax(100);
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100);
volbar1.setProgress(value);
volbar1.setOnSeekBarChangeListener(barChange1);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 60, 20);
}});
{
}
}
}

seekbar onProgressChange value updates extremely slow in toast

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Toast.makeText(this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
changeColor(viewOne, progress);
changeColor(viewTwo, progress);
changeColor(viewThree, progress);
changeColor(viewFour, progress);
changeColor(viewFive, progress);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
I am trying to update the value of the seekbar and display it in a toast. however, the toast updates the value extremely slowly. I would not like to use a textbox. Is there a way to accomplish this?
Using popup window.You can customize the view of popup window, like a textview to show the progress.
In method onStartTrackingTouch, show the popup window.
In method onStopTrackingTouch, dismiss the popup window.
In method onProgressChanged, change the progress.
Sample code:
package com.example.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private SeekBar seek;
private PopupWindow window;
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar) findViewById(R.id.seek);
seek.setOnSeekBarChangeListener(this);
window = new PopupWindow(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textview = new TextView(this);
textview.setTextSize(16);
textview.setBackgroundColor(Color.BLACK);
textview.setTextColor(Color.WHITE);
window.setContentView(textview);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
textview.setText("progress : " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.showAsDropDown(seek);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.dismiss();
}
}
Hope it helps.
Declare the toast at class level
Toast mToast;
Initialize the toast in constructor or onCreate
protected void onCreate(Bundle savedInstanceState) {
...
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
then when you need to show toast
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mToast.setText("progress : " + progress);
mToast.show();
}
now your toast updates will be instantaneous.

The application just "stop unexpectedly" when run on the phone

When I run my application on the emulator, it run right and no problem happens. But when I run it on the phone, a problem appear "stop unexpectedly".
In my application:
The main activity (Background) can exchange to 4 activities: LivingRoom, DiningRoom, Wc, Garage. The problem "stop unexpectedly" just happens when i try to go to the DiningRoom activity.
This is my code :
1.Background
package com.example.background;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import at.abraxas.amarino.Amarino;
public class BackgroundActivity extends Activity {
private Button Wc, Dining_Room, Living_Room, Garage;
private Intent D_intent, L_intent, G_intent, W_intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Amarino.connect(this, DEVICE_ADDRESS);
Living_Room = (Button) findViewById(R.id.living);
//Living_Room.setBackgroundColor(Color.TRANSPARENT);
Living_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
L_intent = new Intent(view.getContext(), LivingRoom.class);
startActivityForResult(L_intent, 0);
}
});
Dining_Room = (Button) findViewById(R.id.dining);
// Dining_Room.setBackgroundColor(Color.TRANSPARENT);
Dining_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
D_intent = new Intent(view.getContext(), DiningRoom.class);
startActivityForResult(D_intent, 0);
}
});
Wc = (Button) findViewById(R.id.wc);
//Wc.setBackgroundColor(Color.TRANSPARENT);
Wc.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
W_intent = new Intent(view.getContext(), Wc.class);
startActivityForResult(W_intent, 0);
}
});
Garage = (Button) findViewById(R.id.garage);
// Garage.setBackgroundColor(Color.TRANSPARENT);
Garage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
G_intent = new Intent(view.getContext(), Garage.class);
startActivityForResult(G_intent, 0);
}
});
}
}
2.DiningRoom
package com.example.background;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;
import at.abraxas.amarino.Amarino;
public class DiningRoom extends Activity implements OnCheckedChangeListener, OnSeekBarChangeListener{
private static final String DEVICE_ADDRESS = "00:06:66:43:9B:56";
final int DELAY = 150;
ToggleButton button;
SeekBar seekbar1;
SeekBar seekbar2;
boolean light2;
int light1;
int fan;
long lastchange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diningroom);
//back ve trang chinh
Button backhome = (Button) findViewById(R.id.D_backhome);
backhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
Amarino.connect(this, DEVICE_ADDRESS);
button = (ToggleButton) findViewById(R.id.Dbutton);
seekbar1 = (SeekBar) findViewById(R.id.Dseekbar1);
seekbar2 = (SeekBar) findViewById(R.id.Dseekbar2);
button.setOnCheckedChangeListener(this);
seekbar1.setOnSeekBarChangeListener(this);
seekbar2.setOnSeekBarChangeListener(this);
}
//---START----------------------------------------------------------------------------------------------
#Override
protected void onStart(){
super.onStart();
// load last state
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
light2 = pref.getBoolean("light2", true);
light1 = pref.getInt("light1", 0);
fan = pref.getInt("fan", 0);
button.setChecked(light2);
seekbar1.setProgress(light1);
seekbar2.setProgress(fan);
new Thread(){
public void run(){
try{
Thread.sleep(6000);
}catch (InterruptedException e) {}
update_light2();
update_light1_fan();
}
}.start();
}
//STOP--------------------------------------------------------------------------------
#Override
protected void onStop(){
super.onStop();
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putBoolean("light2", light2)
.putInt("light1", light1)
.putInt("fan", fan)
.commit();
Amarino.disconnect(this, DEVICE_ADDRESS);
}
//UPDATE LIGHT2--------------------------------------------------------------------------------
private void Update_Light2_State(final CompoundButton button){
light2 = button.isChecked();
update_light2();
}
private void update_light2(){
int a;
a = (light2 == true)? 255:0;
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'o', a);
}
//UPDATE LIGHT1 FAN-------------------------------------------------------------------------------------
private void Update_Light1_Fan_State(final SeekBar seekbar){
switch (seekbar.getId()){
case R.id.Dseekbar1:
light1 = seekbar.getProgress();
update_light1();
break;
case R.id.Dseekbar2:
fan = seekbar.getProgress();
update_fan();
break;
}
}
private void update_light1_fan(){
update_light1();
update_fan();
}
private void update_light1(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', light1);
}
private void update_fan(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'q', fan);
}
//---TRACE--------------------------------------------------------------------------------------
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (System.currentTimeMillis() - lastchange > DELAY ){
Update_Light2_State(button);
lastchange = System.currentTimeMillis();
}
}
#Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
if (System.currentTimeMillis() - lastchange > DELAY ){
Update_Light1_Fan_State(seekbar);
lastchange = System.currentTimeMillis();
}
}
#Override
public void onStartTrackingTouch(SeekBar seekbar) {
lastchange = System.currentTimeMillis();
}
#Override
public void onStopTrackingTouch(SeekBar seekbar) {
Update_Light1_Fan_State(seekbar);
}
}
3.Logcat
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.background/com.example.background.DiningRoom}: java.lang.ClassCastException:
java.lang.Boolean
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Boolean
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2721)
at com.example.background.DiningRoom.onStart(DiningRoom.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
... 11 more
Force finishing activity com.example.background/.DiningRoom
Force finishing activity com.example.background/.BackgroundActivity
I also try to wrote another application similar to the above application. But I just have only 1 sub activity: DiningRoom, then it ran well on the phone.
The Background when I just have one sub activity
[CODE]
public class Test1Activity extends Activity {
private Button Dining_Room;
private Intent D_intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dining_Room = (Button) findViewById(R.id.dining);
// Dining_Room.setBackgroundColor(Color.TRANSPARENT);
Dining_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
D_intent = new Intent(view.getContext(), Dining.class);
startActivityForResult(D_intent, 0);
}
});
}
}
[/CODE]
Careful reading of logcat reveals that:
Caused by: java.lang.ClassCastException: java.lang.Boolean
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2721)
at com.example.background.DiningRoom.onStart(DiningRoom.java:80)
This means, that shared preference was boolean instead of int on line 80.
PS: I develop lightweight dependency injection framework for android, and it already covers
loading / saving preferences. Just grab it here:
https://github.com/ko5tik/andject
I would guess that the value "light1" in your shared preferences was not saved as an integer (but as a boolean).
From the docs:
Throws ClassCastException if there is a preference with this name that
is not an int.

Categories

Resources