无法实例化活动组件信息

时间:2012-10-30 15:39:59

标签: java android

当我运行程序时,我收到运行时错误。无法实例化活动组件信息。

这是我使用的代码,我试图为spinner编写一个listner。

public void addListenerlist() {


        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                String s = arg0.getItemAtPosition(arg2).toString();

                if (s.equals("Malaysia")) {
                    message = getResources().getString(R.string.listdesc1);
                } else if (s.equals("United States")) {
                    message = getResources().getString(R.string.listdesc1);
                } else if (s.equals("Sri Lanka")) {
                    message = getResources().getString(R.string.listdesc2);
                } else if (s.equals("Indonesia")) {
                    message = getResources().getString(R.string.listdesc3);
                } else if (s.equals("France")) {
                    message = getResources().getString(R.string.listdesc4);
                } else if (s.equals("Italy")) {
                    message = getResources().getString(R.string.listdesc5);
                } else if (s.equals("Singapore")) {
                    message = getResources().getString(R.string.listdesc6);
                } else if (s.equals("New Zealand")) {
                    message = getResources().getString(R.string.listdesc7);
                } else if (s.equals("India")) {
                    message = getResources().getString(R.string.listdesc8);
                } else {
                    message = getResources().getString(R.string.listdesc9);
                }

                String listtitle1 = getResources().getString(
                        R.string.listtitle1);
                // String message =
                // getResources().getString(R.string.listdesc1);

                // txtview.setText(message);
                // txtview2.setText(listtitle1);
                Toast.makeText(Page5SubActivity.this,
                        "OnClickListener : " + message , Toast.LENGTH_SHORT)
                        .show();

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });


    }

我是java的新手,另一个问题是,我不能将消息用作局部变量而不是类变量。我使用message varable作为Class变量,因为将它用作局部变量会显示错误。

 10-31 05:18:04.605: E/AndroidRuntime(854): FATAL EXCEPTION: main
    10-31 05:18:04.605: E/AndroidRuntime(854): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xxx.xx/com.xxx.xx.Page5SubActivity}: java.lang.NullPointerException
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.os.Handler.dispatchMessage(Handler.java:99)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.os.Looper.loop(Looper.java:123)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at java.lang.reflect.Method.invoke(Method.java:507)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at dalvik.system.NativeStart.main(Native Method)
    10-31 05:18:04.605: E/AndroidRuntime(854): Caused by: java.lang.NullPointerException
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.Activity.findViewById(Activity.java:1647)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at com.meegua.zakat.Page5SubActivity.<init>(Page5SubActivity.java:17)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at java.lang.Class.newInstanceImpl(Native Method)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at java.lang.Class.newInstance(Class.java:1409)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    10-31 05:18:04.605: E/AndroidRuntime(854):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
    10-31 05:18:04.605: E/AndroidRuntime(854):  ... 11 more

1 个答案:

答案 0 :(得分:0)

我花了好几个小时就找到了问题。 我没有正确地声明类变量。这是错误"unable to instantiate activity component info"

的原因
private Spinner spinner1; 
private TextView txtv ;

我没有声明私有关键字。 认为这对某人有帮助。