Android Spinner小部件

时间:2012-05-06 18:15:11

标签: android

我正在为Android手机开发一种新的应用程序。 我的Spinner小部件有问题,我认为是因为类和Vars,但我不知道它是什么是真正的问题......这是代码:

public class BHac extends Activity 
 {

String[] tzIDs = new String[10000];
String sText;

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

    try
    {
        tzIDs = TimeZone.getAvailableIDs();

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, tzIDs);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setSelection(1);
        spinner.setAdapter(spinnerArrayAdapter);

        try 
        {

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
            {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
                {

                    sText= tzIDs[pos];

                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

                    String timestamp = formatter.format(TimeZone.getTimeZone(sText));

                    TextView t = (TextView)findViewById(R.id.textView2);
                    t.setText(timestamp);
                }

                public void onNothingSelected(AdapterView<?> parent) 
                {
                }
            });
        }
        catch (Exception x)
        {
            Context context = getApplicationContext();
            CharSequence text =  "Android Exception [BHAC]" + x;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            Log.e ("WHWJDAKDJKAJKLDJSKKKKKK", "dadada" + x);
        }

这是LogCat:

05-06 19:53:50.950: E/global(334): Deprecated Thread methods are not supported.
05-06 19:53:50.950: E/global(334): java.lang.UnsupportedOperationException
05-06 19:53:50.950: E/global(334):  at java.lang.VMThread.stop(VMThread.java:85)
05-06 19:53:50.950: E/global(334):  at java.lang.Thread.stop(Thread.java:1280)
05-06 19:53:50.950: E/global(334):  at java.lang.Thread.stop(Thread.java:1247)
05-06 19:53:50.950: E/global(334):  at smale.app.dc.SmaleActivity$1.run(SmaleActivity.java:58)
05-06 19:53:51.748: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 62K, 53% free 2569K/5379K, external 2034K/2137K, paused 98ms
05-06 19:56:36.559: D/AndroidRuntime(334): Shutting down VM
05-06 19:56:36.559: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-06 19:56:36.659: E/AndroidRuntime(334): FATAL EXCEPTION: main
05-06 19:56:36.659: E/AndroidRuntime(334): java.lang.IllegalArgumentException
05-06 19:56:36.659: E/AndroidRuntime(334):  at java.text.DateFormat.format(DateFormat.java:365)
05-06 19:56:36.659: E/AndroidRuntime(334):  at java.text.Format.format(Format.java:93)
05-06 19:56:36.659: E/AndroidRuntime(334):  at smale.app.dc.BHac$1.onItemSelected(BHac.java:62)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.widget.AdapterView.access$200(AdapterView.java:42)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.os.Handler.handleCallback(Handler.java:587)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
05-06 19:56:36.659: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-06 19:56:36.659: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 19:56:36.659: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
05-06 19:56:36.659: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-06 19:56:36.659: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-06 19:56:36.659: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)

有人有想法吗?

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为findViewById返回null将是我的猜测。

没有进一步的代码/布局Indo并不多。可以推断出更多。

答案 1 :(得分:0)

这是抛出异常的行:

String timestamp = formatter.format(TimeZone.getTimeZone(sText));

使用您的代码,我相信这就是您的意图:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
formatter.setTimeZone(sText);

String timestamp = formatter.format(Calendar.getInstance().getTime());