0

我认为我有 3 个微调器。第一个微调器具有固定的初始值。其余部分最初是空的。

当我从第一个微调器中选择一个值时,会根据第一个微调器的选择将新值添加到微调器 2 和 3。

在这里,我只为两个 Spinners 做了一些事情!没有任何代码错误,但我得到了运行时错误,它列在代码下方!谁能帮我?提前致谢 ! 编码

    import java.util.ArrayList;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;

    import com.gtustudents.R;
    import com.gtustudents.common.BaseActivity;
    import com.gtustudents.login.HomePage;

    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;

    import android.widget.Spinner;
    import android.widget.Toast;


    public class demo extends BaseActivity  {
        public Spinner spinner1;
        public Spinner spinner2;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.MY_LAYOUT);

             spinner1= (Spinner)findViewById(R.id.degree);
                 spinner1.setOnItemSelectedListener(new spinnerListen());


            }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)  {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                startActivity(new Intent(this,HomePage.class));
                finish();
                return true;
            }
             return super.onKeyDown(keyCode, event);
        }   
    }


    class spinnerListen extends BaseActivity implements OnItemSelectedListener{
        public Spinner spinner2;


        public void onItemSelected(AdapterView<?> parent, View v, int pos,long id) {
            // TODO Auto-generated method stub
            //use the selected station and departure time to calculate the required time

 Toast toast = Toast.makeText(parent.getContext(),"You've chosen: " + parent.getItemAtPosition(pos), 2);
                toast.show();

                String str = (String) parent.getSelectedItem();
            Log.d("Select Item", str);

                if(str.equals("SOME_VALUE"))
                {
                    Log.d("Enter","YES");

                    final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
                    Log.d("Enter","ArrayAdapter");

                    dataAdapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_spinner_item, items2);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    Log.d("BE Enter","ArrayAdapter -2 ");
                    spinner2.setAdapter(dataAdapter);

                }


        }

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

        }

    }

运行时错误

08-30 16:49:18.586: ERROR/AndroidRuntime(865): FATAL EXCEPTION: main
08-30 16:49:18.586: ERROR/AndroidRuntime(865): java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.app.Activity.getSystemService(Activity.java:3526)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.widget.AdapterView.access$200(AdapterView.java:42)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.os.Handler.handleCallback(Handler.java:587)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.os.Looper.loop(Looper.java:123)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at java.lang.reflect.Method.invoke(Method.java:521)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 16:49:18.586: ERROR/AndroidRuntime(865):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

在您的第二类 {{ spinnerListen }} 中,您已经声明了一个名为 spinner2 的飞行器,它从未启动过,但您必须为其设置一个适配器。

我还没有尝试过,但我想这就是框架假设你还没有通过 onCreate 的原因。

    import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.gtustudents.R;
import com.gtustudents.common.BaseActivity;
import com.gtustudents.login.HomePage;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;

import android.widget.Spinner;
import android.widget.Toast;


public class demo extends BaseActivity  implements OnItemSelectedListener{
    public Spinner spinner1;
    public Spinner spinner2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.MY_LAYOUT);

        spinner1= (Spinner)findViewById(R.id.degree);
        spinner1.setOnItemSelectedListener(this);

        spinner2 = (Spinner)findViewById(R.id.degree2); // TODO reference Proper id


        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            startActivity(new Intent(this,HomePage.class));
            finish();
            return true;
        }
         return super.onKeyDown(keyCode, event);
    }   
}

public void onItemSelected(AdapterView<?> parent, View v, int pos,long id) {
        // TODO Auto-generated method stub
        //use the selected station and departure time to calculate the required time

            Toast toast = Toast.makeText(parent.getContext(),"You've chosen: " + parent.getItemAtPosition(pos), 2);
            toast.show();

            String str = (String) parent.getSelectedItem();
            Log.d("Select Item", str);

            if(str.equals("SOME_VALUE"))
            {
                Log.d("Enter","YES");

                final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
                Log.d("Enter","ArrayAdapter");

                dataAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_spinner_item, items2);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                Log.d("BE Enter","ArrayAdapter -2 ");
                spinner2.setAdapter(dataAdapter);

            }


    }

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

    }
于 2013-09-06T12:18:37.300 回答