0

因此,在我的 android 应用程序中,我使用阿拉伯语,在每个 Activity 类中,我使用了一个方法addtranslate,并在需要翻译 Activity 中的文本时调用它。大约有 15 个活动类,我必须编写该方法并调用它。它工作得很好,但是在我创建一个新类的每个类中实现该方法并在 Activity 类中实例化它并尝试使用它来处理来自 android 项目中一个位置的翻译并不是一个好习惯。

但是当我尝试这个时出现以下错误。这是原木猫

12-18 10:12:26.690: E/AndroidRuntime(341): FATAL EXCEPTION: main
12-18 10:12:26.690: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxx/com.xxxx.xxx.ShowMessageActivity}: java.lang.NullPointerException
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-18 10:12:26.690: E/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 10:12:26.690: E/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:507)
12-18 10:12:26.690: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-18 10:12:26.690: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-18 10:12:26.690: E/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method)
12-18 10:12:26.690: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
12-18 10:12:26.690: E/AndroidRuntime(341):  at com.xxxx.xxx.AddTranslate.addTranslates(AddTranslate.java:11)
12-18 10:12:26.690: E/AndroidRuntime(341):  at com.xxxx.xxx.ShowMessageActivity.onCreate(ShowMessageActivity.java:41)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-18 10:12:26.690: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-18 10:12:26.690: E/AndroidRuntime(341):  ... 11 more

这是新创建的用于翻译的java类

import android.app.Activity;
import android.graphics.Typeface;
import android.widget.TextView;

public class AddTranslate extends Activity {


    public void addTranslates(int rid, TextView txt1) {
        String textv = getResources().getString(rid);
        txt1.setText(Farsi.Convert(textv));
        Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/DroidSansArabic.ttf");
        txt1.setTypeface(typeFace);

    }

}

这就是我在其他活动中使用它的方式

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page17sub);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        final TextView txt1 = (TextView) findViewById(R.id.textView1);
        final TextView txt2 = (TextView) findViewById(R.id.textView2);


        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        AddTranslate tra1   =new AddTranslate();
        AddTranslate tra2   =new AddTranslate();

           if (id == 3) {

            //addTranslate(R.string.butt3title1, txt1);
            //addTranslate(R.string.butt3desc1, txt2);
            tra1.addTranslates(R.string.butt3title1, txt1);
            tra2.addTranslates(R.string.butt3desc1, txt2);

        } else if (id == 5) {

任何人都可以帮我纠正这个问题。

这是将当前活动传递给 addtranslation 对象后的最新日志

12-18 11:40:46.639: E/AndroidRuntime(481): FATAL EXCEPTION: main
12-18 11:40:46.639: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxx/com.xxxx.xxx.ShowMessageActivity}: java.lang.NullPointerException
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:123)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-18 11:40:46.639: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 11:40:46.639: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:507)
12-18 11:40:46.639: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-18 11:40:46.639: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-18 11:40:46.639: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)
12-18 11:40:46.639: E/AndroidRuntime(481): Caused by: java.lang.NullPointerException
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
12-18 11:40:46.639: E/AndroidRuntime(481):  at com.xxxx.xxx.AddTranslate.addTranslates(AddTranslate.java:19)
12-18 11:40:46.639: E/AndroidRuntime(481):  at com.xxxx.xxx.ShowMessageActivity.onCreate(ShowMessageActivity.java:43)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-18 11:40:46.639: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-18 11:40:46.639: E/AndroidRuntime(481):  ... 11 more
4

1 回答 1

0

您将需要使用 AddTranslate 的构造函数在 AddTranslate ShowMessageActivity 活动中发送当前上下文,将您的代码更改为:

public class AddTranslate {
public Context context;

public AddTranslate(Context context){
this.context=context;
}

    public void addTranslates(int rid, TextView txt1) {
        String textv = context.getResources().getString(rid);


        txt1.setText(Farsi.Convert(textv));
        Typeface typeFace=Typeface.createFromAsset(context.getAssets(),
                                            "fonts/DroidSansArabic.ttf");
        txt1.setTypeface(typeFace);

    }

}

并来自 ShowMessageActivity Activity :

  AddTranslate tra1   =new AddTranslate(Current_Activity.this);
    AddTranslate tra2   =new AddTranslate(Current_Activity.this);
于 2012-12-18T05:27:20.610 回答