从另一个给出异常的类调用一个微调器

时间:2012-05-04 13:33:17

标签: android spinner

我有两个类:DineOutActivity.java和Userpersonal .java 我有一个在DineOutActivity.java中定义的微调器(spinner1)

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

并调用spinner方法。因此,当我从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(){

1 个答案:

答案 0 :(得分:1)

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

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

请参阅 http://developer.android.com/reference/android/app/Activity.html

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