0

我正在尝试填充微调器,但出现错误。这是我的代码...

private ArrayList<String> ctRatioList = new ArrayList<String>();
private ArrayList<String> rVoltsList = new ArrayList<String>();

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

    ctRatioList.add( "800:5" );
    ctRatioList.add( "1200:5" );
    ctRatioList.add( "1600:5" );
    ctRatioList.add( "2000:5" );
    ctRatioList.add( "2500:5" );
    ctRatioList.add( "3000:5" );
    ctRatioList.add( "3500:5" );
    ctRatioList.add( "4000:5" );
    ctRatioList.add( "4500:5" );        
    Spinner ctRatioSpinner = (Spinner) findViewById( R.id.ctRatioSpinner );
    ArrayAdapter<String> ctRatioAdapter = new ArrayAdapter<String>( this, R.layout.parameters, ctRatioList );
    ctRatioSpinner.setAdapter( ctRatioAdapter );


    for( double i = 0; i < 10 ; i+=0.1 ) {
        rVoltsList.add( Double.toString( i ) );
    }
    Spinner rVoltsSpinner = (Spinner) findViewById( R.id.recloseVoltsSpinner );
    ArrayAdapter<String> rVoltsAdapter = new ArrayAdapter<String>( this, R.layout.parameters, rVoltsList );
    rVoltsSpinner.setAdapter( rVoltsAdapter );
}

这是我的 LogCat ...

06-26 09:39:30.235: W/dalvikvm(7711): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-26 09:39:30.259: E/AndroidRuntime(7711): FATAL EXCEPTION: main
06-26 09:39:30.259: E/AndroidRuntime(7711): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.TableRow.getColumnsWidths(TableRow.java:308)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.TableLayout.measureVertical(TableLayout.java:465)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.TableLayout.onMeasure(TableLayout.java:431)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1074)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.ScrollView.onMeasure(ScrollView.java:296)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:8313)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.os.Looper.loop(Looper.java:130)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at java.lang.reflect.Method.invoke(Method.java:507)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 09:39:30.259: E/AndroidRuntime(7711):     at dalvik.system.NativeStart.main(Native Method)
06-26 09:39:30.259: E/AndroidRuntime(7711): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
06-26 09:39:30.259: E/AndroidRuntime(7711):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
06-26 09:39:30.259: E/AndroidRuntime(7711):     ... 36 more
4

2 回答 2

1

异常显示“ArrayAdapter 要求资源 ID 为 TextView”

在此调用中,资源R.layout.parameters不是文本视图,而是完整的布局:

ArrayAdapter<String> ctRatioAdapter = new ArrayAdapter<String>( this, R.layout.parameters, ctRatioList );

你应该使用类似的东西android.R.layout.simple_spinner_item。请参阅 Android 文档中的Spinner 教程

于 2012-06-26T13:48:27.327 回答
0

一旦您的适配器膨胀布局,在此布局R.layout.parameters中不包含任何TextView检查。

于 2012-06-26T13:48:38.067 回答