0

when I run the programme I get a runtime error. Unable to instantiate activity component info.

Here is the code I use, I tried to write a listner for spinner.

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

            }

        });


    }

I am new to java, another problem is that , can't I use message as local variable instead of a class variable. I have use message varable as a Class variable since using it as a local variable shows errors.

 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
4

1 回答 1

0

在花了几个小时后,我可以找到问题所在。我没有正确声明类变量。这是错误的原因"unable to instantiate activity component info"

private Spinner spinner1; 
private TextView txtv ;

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

于 2012-10-30T20:02:23.190 回答