蓝牙应用程序崩溃,因为ArrayAdapter要求资源ID必须是一个TextView。

14 浏览
0 Comments

蓝牙应用程序崩溃,因为ArrayAdapter要求资源ID必须是一个TextView。

当我尝试将视图设置为显示我想要显示的文件(文本文件)的ListView时,出现了错误。我很确定这与XML有关。我只想显示来自this.file = fileop.ReadFileAsList(\"Installed_packages.txt\");的信息。我的代码如下:\n

public class Main extends Activity {
    private TextView tv;
    private FileOperations fileop;
    private String[] file;
    /** 当活动被创建时调用 */       
    @Override
    public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState); 
        this.fileop = new FileOperations(); 
        this.file = fileop.ReadFileAsList("Installed_packages.txt"); 
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);
        ListView lv = new ListView(this);
        lv.setTextFilterEnabled(true); 
        lv.setAdapter(new ArrayAdapter(this, R.layout.list_item, this.file)); 
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
              public void onItemClick(AdapterView parent, View view,     int position, long id) { 
                    // 当点击时,显示一个带有TextView文本的toast
                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
              } 
        });         
        setContentView(lv);
    }
}

\nlist_item.xml :\n




\nmain.xml :\n




        
    

0
0 Comments

问题原因:出现这个错误是因为在扩展ArrayAdapter时,没有提供正确的资源ID来显示项目。

解决方法:在构造函数中调用超类并传入TextView的资源ID。

//Pass in the resource id:  R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
        R.id.text_view,
        new ArrayList<>());

适配器:

public class SpinnerAdapter extends ArrayAdapter {
    private Context context;
    private List values;
    public SpinnerAdapter(Context context, int textViewResourceId,
                          List values) {
        //Pass in the resource id:  R.id.text_view
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;
    }
}

以上是解决Bluetooth应用程序崩溃的问题,原因是ArrayAdapter需要资源ID为TextView。

0
0 Comments

问题的原因是ArrayAdapter需要将资源ID设置为一个TextView,而listitem.xml文件中没有正确设置TextView的资源ID。解决方法是在listitem.xml文件中为TextView设置一个正确的资源ID。

解决方法如下:

1. 打开listitem.xml文件;

2. 在TextView标签中添加android:id="@+id/textview",为TextView设置一个正确的资源ID;

3. 保存并关闭listitem.xml文件。

修改后的listitem.xml文件如下:



     
     

修改后的Java代码如下:

String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.listitem, R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

这样就解决了Bluetooth app crashed due to ArrayAdapter requires the resource ID to be a TextView这个问题。

0
0 Comments

这个问题的出现原因是ArrayAdapter需要资源ID是一个TextView的XML。解决方法是使用正确的构造函数来创建ArrayAdapter,使其能够正确地处理布局文件中的TextView。

当使用以下构造函数时:

new ArrayAdapter(this, R.layout.a_layout_file, this.file)

R.layout.a_layout_file必须是一个只包含一个TextView的XML布局文件,不能包含其他的布局。例如:

android:layout_width="fill_parent"

android:layout_height="wrap_content"

// 其他TextView的属性

/>

如果你希望列表的行布局不仅仅是一个简单的TextView小部件,可以使用以下构造函数:

new ArrayAdapter(this, R.layout.a_layout_file, R.id.the_id_of_a_textview_from_the_layout, this.file)

在这种情况下,你需要提供一个包含多个视图的布局的id,但是也必须包含一个TextView并且有一个id(作为第三个参数)传递给ArrayAdapter,这样它就知道在行布局中放置字符串的位置。

如果你的TextView被包裹在LinearLayout之类的布局中,就会出现这个问题。可以使用另一个构造函数new ArrayAdapter(this, R.layout.a_layout_file, R.id.a_text_view_within_layout, this.file)来解决这个问题。

ArrayAdapter是一个非常简单的适配器,它将一个数据绑定到一个单独的TextView。如果布局中有其他视图,有两种情况:1.那些视图是静态的(不需要设置数据),在这种情况下,可以使用我在答案中提到的方法。2.如果需要在其他视图上设置数据,那么就需要重写ArrayAdapter来自己进行绑定。在getView()方法中可以获取到其他视图,不需要传递这些视图。

在getView()方法中,如果遇到了类似holder.idProf.setText(item.getId_prof())这样的问题,可能是因为TextView的资源ID没有找到。可以确认一下TextView是否包含在RelativeLayout中。

通过使用正确的构造函数和布局文件,可以解决这个问题。感谢提供帮助的人。

0