1

我查看了几个线程,其中提出的任何建议似乎都无法解决我遇到的问题。我在同一活动中有 2 个旋转器。我从一个微调器中选择的值用于填充另一个微调器,然后我想从第二个微调器中选择一个值。它一直有效,直到我在动态填充后单击第二个微调器,然后应用程序崩溃。我不确定还能做什么。我将发布我拥有的相关代码以及我收到的错误消息。

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bodyregionandpart_layout);
    bodyRegion = (Spinner) findViewById(R.id.bodyregion);
    // Create an ArrayAdapter using the string array and a default spinner layout
    bodyRegionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bodyregion);
    // Specify the layout to use when the list of choices appears
    bodyRegionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    bodyRegion.setAdapter(bodyRegionAdapter);
    //Sets the spinner to listen when an item is selected
    bodyRegion.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) 
{
     if(parent.getId() == R.id.bodyregion)
     {
         switch(pos)
            {
            case 1:
                FillSpinner(Head);
                Log.d("bodyregion", "This is Head");
                break;
            }                    
     }
     else if(parent.getId() == R.id.bodypart)
     {
         Log.d("bodypart", "This is body part: " + (String) parent.getItemAtPosition(pos));
     }
}

public void onNothingSelected(AdapterView<?> parent) 

{
    // Another interface callback
}

public void FillSpinner(String[] a)
{
    //Fills array with contents of a
    for(int i = 0; i < a.length; i++)
    {
        array[i] = a[i];
    }
    bodyPart = (Spinner) findViewById(R.id.bodypart);
    // Create an ArrayAdapter using the string array and a default spinner layout
    bodyPartAdapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_item, array);
    // Specify the layout to use when the list of choices appears
    bodyPartAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    bodyPart.setAdapter(bodyPartAdapter);
    bodyPart.setOnItemSelectedListener(this);
}

然后这是我收到的错误消息:

01-21 22:10:29.616: D/bodyregion(2855): This is Head
01-21 22:10:29.715: D/bodypart(2855): This is body part: Choose Body Part
01-21 22:10:31.385: D/AndroidRuntime(2855): Shutting down VM
01-21 22:10:31.385: W/dalvikvm(2855): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-21 22:10:31.415: E/AndroidRuntime(2855): FATAL EXCEPTION: main
01-21 22:10:31.415: E/AndroidRuntime(2855): java.lang.NullPointerException
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:725)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:721)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.Spinner.measureContentWidth(Spinner.java:669)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.Spinner$DropdownPopup.show(Spinner.java:948)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.widget.Spinner.performClick(Spinner.java:597)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.view.View$PerformClick.run(View.java:16966)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.os.Handler.handleCallback(Handler.java:615)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.os.Looper.loop(Looper.java:137)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-21 22:10:31.415: E/AndroidRuntime(2855):     at dalvik.system.NativeStart.main(Native Method)

第二条日志消息仅显示微调器创建时的第一个选择。一旦我单击它以选择其他内容,它就会崩溃。任何帮助/建议表示赞赏。谢谢。

4

1 回答 1

1

做小的改变..检查数组的长度是否大于0然后只将适配器设置为第二个微调器

public void FillSpinner(String[] a)
    {
        //Fills array with contents of a
        for(int i = 0; i < a.length; i++)
        {
            array[i] = a[i];
        }
        if(array.length>0)
        {
            bodyPart = (Spinner) findViewById(R.id.bodypart);
            // Create an ArrayAdapter using the string array and a default spinner layout
            bodyPartAdapter = new ArrayAdapter<String>(this, 
                    android.R.layout.simple_spinner_item, array);
            // Specify the layout to use when the list of choices appears
            bodyPartAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            bodyPart.setAdapter(bodyPartAdapter);
            bodyPart.setOnItemSelectedListener(this);
        }

    }
于 2013-01-22T03:20:49.867 回答