无法启动组件信息android

时间:2012-12-18 05:17:02

标签: android class

在我的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) {

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

这是将currnt活动传递给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

1 个答案:

答案 0 :(得分: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);