arrayadapter:您必须为textview-android studio提供一个资源id

z4iuyo4d  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(401)

这个问题在这里已经有答案了

必须为textview android错误提供资源id(4个答案)
四年前关门了。
我正在尝试使用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);

渐变文件:

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')
}
b5lpy0ml

b5lpy0ml1#

第三个参数 ArrayAdapter#createFromResource 如果我正确理解文档、示例和错误消息,应该是一个只有文本视图的布局的标识符。例如,使用 android.R.layout.simple_spinner_item .
问题是你使用的布局( andoid.R.layout.activity_list_item )将relativelayout作为根视图。

7xzttuei

7xzttuei2#

“android.r.layout.activity\u list\u item”是一个线性布局。createfromresource(..)需要textview作为其第三个参数。尝试改用“android.r.layout.simple\u list\u item\u 1”。布局应该只有textview,而不应该 Package 在linearlayout中。

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

dohp0rv53#

看一下文件。createfromresource()的最后一个参数必须是 TextView :
在layouts文件夹中创建此my\u item\u 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);

相关问题