I have a Runnable class that ends up changing the text and color of a textview depending on a certain value that it gets from the SharedPreferences.
I tried creating the class inside a class that is calling the runnable at first, that worked fine, Now I wanted to put this class inside it's own class file to keep it clean and to be able to access it easily from other classes.
However I get a nullpointer now and after commenting some code out it seems like the textview is causing the problem.
This is my class
public class SettingsRunnable extends Activity implements Runnable {
View mView;
SharedPreferences mSettings;
SharedPreferences.Editor mEditSettings;
TextView mText;
CardScrollView mCardScroller;
public SettingsRunnable(View view, TextView text, SharedPreferences settings, CardScrollView cardView)
{
this.mView = view;
this.mText = text;
this.mSettings = settings;
this.mCardScroller = cardView;
mEditSettings = mSettings.edit();
}
@Override
public void run() {
try {
if(mView == null)
{
mView = mCardScroller.getSelectedView();
}
String status = "";
if (mSettings.contains("headgesture")) {
status = mSettings.getString("headgesture", "");
mText.setText(status);
switch (status) {
case "on":
mText.setTextColor(getResources().getColor(R.color.status_on));
break;
case "off":
mText.setTextColor(getRecources().getColor(R.color.status_off));
break;
}
}
if (mSettings.contains("network")) {
status = mSettings.getString("network", "");
//mText.setText(status);
switch (status) {
case "always":
break;
case "wifi":
break;
case "never":
break;
}
}
mView.setTag(mText);
}
catch(Exception e)
{
Log.wtf("ErrorRun", e.getMessage());
}
}
}
Then I've the following in my onCreate() in the class that is trying to call it.
settings = getSharedPreferences(PREFERENCE, PRIVATE);
view = new View(this);
mText = (TextView) findViewById(R.id.headgesture_status);
setR = new SettingsRunnable(view, mText, settings, mCardScroller);
handler.postDelayed(setR, 1000);
0 comments:
Post a Comment