最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android代码出错:The method setListAdapter(ArrayAdapter<String>) is undefined for the type xxx

Android crifan 3537浏览 0评论

【问题】

新建一个Activity:

【记录】ADT中创建Android的Activity

然后去实现对应的onCreate中的初始化:

import android.app.Activity;

public class DirectoryBrowser extends Activity {

    private List<String> items = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_directory_browser);
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		getFiles(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/").listFiles());
	}
	
    private void getFiles(File[] files){
        items = new ArrayList<String>();
        items.add(getString(R.string.goto_root));
        for(File file : files){
            items.add(file.getPath());
        }
        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_list_row, items);
        setListAdapter(fileList);
    }
}

结果出错:

The method setListAdapter(ArrayAdapter<String>) is undefined for the type DirectoryBrowser

The method setListAdapter ArrayAdapter String  is undefined for the type

 

【解决过程】

1.后来参考:

Android for Dummies code error

才注意到,之前别人的代码是:

public class DirectoryBrowser extends ListActivity {

所以去把代码改为对应的效果,然后添加上import,就变成了:

//import android.app.Activity;
import android.app.ListActivity;

public class DirectoryBrowser extends ListActivity {
	//public class DirectoryBrowser extends Activity {

    private List<String> items = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_directory_browser);
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		getFiles(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/").listFiles());
	}
	
    private void getFiles(File[] files){
        items = new ArrayList<String>();
        items.add(getString(R.string.goto_root));
        for(File file : files){
            items.add(file.getPath());
        }
        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_list_row, items);
        setListAdapter(fileList);
    }

就可以了。

 

【总结】

当遇到类似于:

The method setListAdapter(ArrayAdapter<String>) is undefined for the type xxx

把对应的类,添加上对应的:

extends ListActivity

其中setListAdapter是属于ListActivity类的;

然后再加上import:

import android.app.ListActivity;

就可以了。

转载请注明:在路上 » 【已解决】Android代码出错:The method setListAdapter(ArrayAdapter<String>) is undefined for the type xxx

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.194 seconds, using 22.15MB memory