在我的應用中我不能重用我已經在ArabicUtility
類中聲明的方法。我想要做的是用Arabicutility
來安排一個阿拉伯語的文本。因此,我需要做的是給已經在Arabicutility
類中聲明的方法傳遞字符串並進行轉換
我想這個在面向對象中是有一些困難的,所以希望能有人幫我改一下。
這個是我在Arabicutility
類中加的方法
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);
}
因為getResources()
是非靜態的,所以我不能將這個方法聲明成靜態的。因為我用的是android方法,所以我不得不從Activity
中擴展。原始的它不是這麼定義的。
這個就是我在其他activity類中用的上邊的方法
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.
這個是 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
不需要聲明為靜態addTranslate獲得資源在非活動類你只需要通過當前活動上下文通過使用非活動類構造函數或通過在方法為:
在non activity類中不需要聲明addTranslate
為靜態來獲得Resources
,你只需要用non activity類構造函數來傳遞 Current Activity Context,或者是在方法中傳遞:
public void addTranslate(int rid, TextView txt1,Context context) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
現在你從activity類中調用addTranslate:
arbic.addTranslate(R.string.butt18title1, txt1,Your_Current_Activity.this);
arbic.addTranslate(R.string.butt18desc1, txt2,Your_Current_Activity.this);