ArrayAdapter:您必须为TextView- Android Studio提供资源ID

时间:2017-01-03 20:38:58

标签: java android android-arrayadapter

我正在尝试使用ArrayAdapter构建货币转换器。但我得到这个错误!有谁知道如何解决这个问题? 我在这里检查了其他帖子,但似乎没有任何效果。 我正在关注youtube上的教程,但开发人员没有遇到此问题。我的一个朋友做了同样的教程,我们有相同的代码,但她的项目工作正常...不知道它有什么不对吗

01-03 20:05:20.242 5183-5183/? E/ArrayAdapter: You must supply a resource ID for a TextView
01-03 20:05:20.243 5183-5183/? D/AndroidRuntime: Shutting down VM
01-03 20:05:20.245 5183-5183/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.android.example.currencyconverter2, PID: 5183
                                                 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                                                     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
                                                     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                                                     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
                                                     at android.widget.Spinner.onMeasure(Spinner.java:580)
                                                     at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:418)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                     at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                     at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                     at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                     at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                     at android.view.View.measure(View.java:18788)
                                                     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                     at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                  Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                                                     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                                                     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 
                                                     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194) 
                                                     at android.widget.Spinner.onMeasure(Spinner.java:580) 
                                                     at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:418) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 
                                                     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                                                     at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                                                     at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                                                     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                                                     at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                                                     at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                                                     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
                                                     at android.view.View.measure(View.java:18788) 
                                                     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
                                                     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                     at android.view.Choreographer.doFrame(Choreographer.java:606) 
                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

private EditText InputCurrency;
private Spinner ConvertFrom;
private Spinner ConvertToC;
private Button btnConvert;
private TextView CFtextView;
private TextView CTtextView;
private String fixer_url ="http://api.fixer.io/latest?symbols="; //USD,GBP
private AQuery aq;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    InputCurrency =(EditText) findViewById(R.id.InputEditText);
    ConvertFrom = (Spinner)findViewById(R.id.fspinner);
    ConvertToC = (Spinner)findViewById(R.id.Tspinner);
    btnConvert = (Button)findViewById(R.id.btnConvert);
    CFtextView = (TextView)findViewById(R.id.CFtextView);
    CTtextView = (TextView)findViewById(R.id.CTtextView);
    aq = new AQuery(this);


            // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.currencies_array,     android.R.layout.activity_list_item);
// Specify the layout to use when the list of choices appears
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
                ConvertFrom.setAdapter(adapter);
                ConvertToC.setAdapter(adapter);

gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.3"

defaultConfig {
    applicationId "com.android.example.currencyconverter2"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile files('libs/android-query-full.0.26.8.jar')
}

3 个答案:

答案 0 :(得分:1)

$.phoneNumbers[0]['bob@gmail.com']的第三个参数应该是布局的标识符,其中只有TextView,如果我正确理解文档,示例和错误消息。 例如,使用ArrayAdapter#createFromResource

问题是你使用的布局(android.R.layout.simple_spinner_item)有一个RelativeLayout作为根视图。

答案 1 :(得分:0)

查看documentation。 createFromResource()的最后一个参数需要是TextView的布局ID:

在layouts文件夹中创建此my_item_layout.xml文件:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/item_text"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center_horizontal"
      android:paddingTop="1dp"
      android:paddingBottom="1dp"
      android:paddingStart="8dp"
      android:paddingEnd="8dp"/>

并在你的适配器

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currencies_array, R.layout.my_item_layout);

答案 2 :(得分:0)

“android.R.layout.activity_list_item”是一个线性布局。 createFromResource(..)期待Textview成为它的第三个参数。请尝试使用“android.R.layout.simple_list_item_1”。布局应该只有TextView,不应该包含在Linearlayout中。

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.currencies_array,android.R.layout.simple_list_item_1);