Java – Method Android OOP cannot be reused
In my application, I cannot reuse the methods I declare in the arabictutility class My intention is to use the Arabic class to arrange the Arabic text So what I want is to pass the string to the method I declare in the arabictutility class and convert it
I think this is basically my knowledge of OOP So help me correct this
This is the method I added to the arabictutility 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 cannot declare this method static because getresources () is non - static I have to extend from activity because I use Android methods There is no such definition in principle
This is how I try to use the above method in other activity classes
arbic.addTranslate(R.string.butt18title1,txt1); arbic.addTranslate(R.string.butt18desc1,txt2);
But when I run the program, it crashes when I do the above activities
This 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
Solution
There is no need to declare addtranslate as static to get the resources in the inactive class. You only need to pass the current active context by using the inactive class constructor or in the method:
public void addTranslate(int rid,TextView txt1,Context context) { String textv = context.getResources().getString(rid); txt1.setText(ArabicUtilities.reshapeSentence(textv)); }
You can now call addtranslate from the activity class:
arbic.addTranslate(R.string.butt18title1,txt1,Your_Current_Activity.this); arbic.addTranslate(R.string.butt18desc1,txt2,Your_Current_Activity.this);