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.
mCards = new ArrayList<>();
//android.os.Debug.waitForDebugger();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
CreateCards();//is being used to add cards to the mCards list, these cards will be inflated in the mainAdapter
mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(new mainAdapter(mCards, getLayoutInflater()));
mCardScroller.setOnItemClickListener(this);
mGestureDetector = createGestureDetector(this);
card = new CardPosition(mCardScroller);//is being used to get the current position of the cardscroller
setContentView(mCardScroller);
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);
Edit
6086-6086/com.example.sw_stage.glass E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sw_stage.glass, PID: 6086
java.lang.NullPointerException
at com.example.sw_stage.glass.Runnables.SettingsRunnable.run(SettingsRunnable.java:42)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Edit 2
It seems that when I try to get the textview in my onCreate it returns null. I'm not completely sure why this is. The view should already be inflated and set at the point that this is being called. Actually when I put in this code to test it quickly it works perfectly. However it's dirty ofcourse
settings = getSharedPreferences(PREFERENCE, PRIVATE);
view = new View(this);
mText = (TextView) findViewById(R.id.headgesture_status);
//handler.postDelayed(setR, 1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
mText = (TextView) findViewById(R.id.headgesture_status);
setR = new SettingsRunnable(view, mText, settings, mCardScroller, false);
handler.postDelayed(setR,0);
}
},0);
XML file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/main_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_headgesture"
android:gravity="center"
android:textSize="47pt"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAllCaps="true"
android:textStyle="bold|italic"
android:gravity="center"
android:textSize="47pt"
android:id="@+id/headgesture_status"/>
</LinearLayout>