1

我有两个类:DineOutActivity.java 和 Userpersonal .java 我在 DineOutActivity.java 中定义了一个 spinner(spinner1)

我正在创建一个方法,以便在 UserPersonal.java 中单击图像

并调用微调器方法。因此,当我从 DineOutActivity.java 单击 spinner1 并进入 User Personal.java 然后单击提交时,它将重定向到另一个页面。

这是我的 DineOutActivity.java

public class DineOutActivity extends Activity {

    Spinner sp;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /********************spinner***********/

        Spinner food = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> foodadapter = ArrayAdapter.createFromResource(
                    this, R.array.item_array, android.R.layout.simple_spinner_item);
        foodadapter.setDropDownViewResource(R.layout.spinner_layout);
        food.setAdapter(foodadapter);
}

这是我的 UsePersonal.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userpersonal);

    }

    public void spinner()
    { DineOutActivity doa = new DineOutActivity() ;
         Spinner food = doa.sp;

            food.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
             public void onItemSelected(AdapterView<?> parent, View view,
                     int pos, long id) {
                if(pos ==3)
                {
                    Intent select = new Intent();
                    select.setClass(getBaseContext(), PizzaHut.class);
                }
}
          @Override
         public void onNothingSelected(AdapterView<?> arg0) {
             // TODO Auto-generated method stub

         }
     });
    }

public void onClick(View view)

{  spinner();
}




05-04 18:49:32.617: E/AndroidRuntime(24237): FATAL EXCEPTION: main
05-04 18:49:32.617: E/AndroidRuntime(24237): java.lang.IllegalStateException: Could not execute method of the activity
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.view.View$1.onClick(View.java:2144)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.view.View.performClick(View.java:2485)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.view.View$PerformClick.run(View.java:9080)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.os.Handler.handleCallback(Handler.java:587)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.os.Looper.loop(Looper.java:130)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at java.lang.reflect.Method.invoke(Method.java:507)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at dalvik.system.NativeStart.main(Native Method)
05-04 18:49:32.617: E/AndroidRuntime(24237): Caused by: java.lang.reflect.InvocationTargetException
05-04 18:49:32.617: E/AndroidRuntime(24237):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at java.lang.reflect.Method.invoke(Method.java:507)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at android.view.View$1.onClick(View.java:2139)
05-04 18:49:32.617: E/AndroidRuntime(24237):    ... 11 more
05-04 18:49:32.617: E/AndroidRuntime(24237): Caused by: java.lang.NullPointerException
05-04 18:49:32.617: E/AndroidRuntime(24237):    at com.crews.dineout.UserPersonal.spinner(UserPersonal.java:63)
05-04 18:49:32.617: E/AndroidRuntime(24237):    at com.crews.dineout.UserPersonal.onClick(UserPersonal.java:83)
05-04 18:49:32.617: E/AndroidRuntime(24237):    ... 14 more

我在 UserPersonal.java food.setOnItemSelectedListener(new OnItemSelectedListener() {

4

1 回答 1

1

您引用的活动是使用关键字“new”创建的
->DineOutActivity doa = new DineOutActivity()

活动有自己的生命周期,并使用意图创建!

http://developer.android.com/reference/android/app/Activity.html

因此,您不会与正确的对象交谈,因为此活动既没有布局,也没有贯穿onCreate()

于 2012-05-04T13:51:07.810 回答