Reputation: 225
I'm following an e-book of android first developing; in a certain point the goal was to change color of each letter cyclically.
the example is written in pieces and it's not clear for me how to glue them well.
NOTE: I inserted the definition of selfchange, in the book it wasn't put.
So where are the errors? in this way my app crash.
Should I put @Override
over the onTextChanged
method?
I wrote like this:
public class CustomTextView extends TextView
{
public CustomTextView(Context context,AttributeSet attributeSet,int defSytle)
{
super(context, attributeSet, defSytle);
}
public CustomTextView(Context context,AttributeSet attributeSet)
{
super(context, attributeSet);
}
public CustomTextView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
protected void onTextChanged(CharSequence text,
int start, int lengthBefore, int lengthAfter)
{
if(selfChange)
{
selfChange = false;
return;
}
selfChange=true;
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.clearSpans();
ForegroundColorSpan colorSpan;
int color;
for(int i=0; i < text.length(); i++)
{
//pick the next color
color = colorArray[i%colorArray.length];
//Create the color span
colorSpan = new ForegroundColorSpan(color);
//Add the color span for this one char
builder.setSpan(colorSpan,
i, i,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
setText(builder);
}
boolean selfChange = false;
int colorArray[] = new int[]
{
Color.WHITE,
Color.RED,
Color.YELLOW,
Color.GREEN,
Color.BLUE,
Color.MAGENTA,
Color.CYAN,
Color.DKGRAY
};
}
LogCat:
01-04 00:17:34.560: D/AndroidRuntime(3149): Shutting down VM
01-04 00:17:34.560: W/dalvikvm(3149): threadid=1: thread exiting with uncaught exception (group=0x40fbe2a0)
01-04 00:17:34.575: E/AndroidRuntime(3149): FATAL EXCEPTION: main
01-04 00:17:34.575: E/AndroidRuntime(3149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lux.myproject/com.lux.myproject.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.os.Looper.loop(Looper.java:137)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-04 00:17:34.575: E/AndroidRuntime(3149): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-04 00:17:34.575: E/AndroidRuntime(3149): at dalvik.system.NativeStart.main(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.Activity.setContentView(Activity.java:1924)
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.lux.myproject.MainActivity.onCreate(MainActivity.java:53)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.Activity.performCreate(Activity.java:5206)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-04 00:17:34.575: E/AndroidRuntime(3149): ... 11 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.reflect.InvocationTargetException
01-04 00:17:34.575: E/AndroidRuntime(3149): at java.lang.reflect.Constructor.constructNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-04 00:17:34.575: E/AndroidRuntime(3149): ... 22 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.NullPointerException
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.lux.myproject.CustomTextView.onTextChanged(CustomTextView.java:46)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.widget.TextView.setText(TextView.java:3723)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.widget.TextView.setText(TextView.java:3577)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.widget.TextView.<init>(TextView.java:1224)
01-04 00:17:34.575: E/AndroidRuntime(3149): at android.widget.TextView.<init>(TextView.java:566)
01-04 00:17:34.575: E/AndroidRuntime(3149): at com.lux.myproject.CustomTextView.<init>(CustomTextView.java:20)
01-04 00:17:34.575: E/AndroidRuntime(3149): ... 25 more
Upvotes: 1
Views: 547
Reputation: 365
protected void onTextChanged(CharSequence text,
int start, int lengthBefore, int lengthAfter)
{
if (text.lenght > 0)
//YOUR CODE
}
Try to check the lenght of the text
screen.setText is breaking on use. uncaught expection (group=0xb3a539@0) (Android)
Upvotes: 1