In my application I can not reuse method I have declared in ArabicUtility class. My intension is to use Arabicutility class to arrange Arabic text. Therefore, what I want is passe string to the method I declared in Arabicutility class and do the conversion.
I think this is basically some problem of my knoweledge of OOP. so help me to correct this.
Here is the method I add to Arabicutility class
public void addTranslate(int rid, TextView txt1) {
String textv = getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
// Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/DroidNaskhBold.ttf");
// txt1.setTypeface(typeFace);
}
I can not declare this method as static since getResources() is a non static.I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);
but When I run the programe it crashes when I go to above activities.
here is the log cat
12-29 10:02:32.561: E/AndroidRuntime(951): FATAL EXCEPTION: main
12-29 10:02:32.561: E/AndroidRuntime(951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxx/com.xxxx.xxx.ShowMessageActivity}: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 10:02:32.561: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): Caused by: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ArabicUtilities.addTranslate(ArabicUtilities.java:252)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ShowMessageActivity.onCreate(ShowMessageActivity.java:184)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Activity.performCreate(Activity.java:5008)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-29 10:02:32.561: E/AndroidRuntime(951): ... 11 more
No need to declare addTranslate as static to get Resources in non Activity class you just need to pass Current Activity Context by using non activity class constructor or passing as in method as :
public void addTranslate(int rid, TextView txt1,Context context) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
Now you can call addTranslate from Activity class as:
arbic.addTranslate(R.string.butt18title1, txt1,Your_Current_Activity.this);
arbic.addTranslate(R.string.butt18desc1, txt2,Your_Current_Activity.this);
The LogCat shows that Context in ArabicUtility is invalid. Try using the TextView's Context instead:
public void addTranslate(int rid, TextView txt1) {
String textv = txt1.getContext().getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
If ArabicUtility is not the active Activity then you shouldn't extend Activity, you should try something like this:
public class ArabicUtility {
private Context context;
public ArabicUtility(Context context) {
this.context = context;
}
...
public void addTranslate(int rid, TextView txt1) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
}
In your current Activity use:
arbic = new ArabicUtility(this);
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With