菜鸟一个,Android开发写一个浏览SD卡中的文件的程序,调试的时候打不开,大神求教,谢谢。 60
日志(部分):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.example.neww...
日志(部分): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newwindow1.projectreadandwrite/com.example.newwindow1.projectreadandwrite.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
Mainactivity.java代码:
public class MainActivity extends Activity {
private ListView mListView;
private TextView mTextView;
private File mCurrentParent;
File[] mCurrentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list);
mTextView = (TextView) findViewById(R.id.path);
File root = new File(String.valueOf(Environment.getExternalStorageDirectory()));
if (root.exists()){
mCurrentParent = root;
mCurrentFiles = root.listFiles();
inflateListView(mCurrentFiles);
}
Button parent = (Button) findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (!mCurrentParent.getCanonicalFile().equals("/mnt/sdcard")){
mCurrentParent = mCurrentParent.getParentFile();
mCurrentFiles = mCurrentParent.listFiles();
inflateListView(mCurrentFiles);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void inflateListView(File[] files) {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(10);
for (int i = 0; i < files.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
if (files[i].isDirectory()){
listItem.put("icon", R.drawable.folder);
}else {
listItem.put("icon", R.drawable.file);
}
listItem.put("fileName", files[i].getName());
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.line, new String[]{"icon", "fileName"}, new int[]{R.id.icon, R.id.file_name});
mListView.setAdapter(simpleAdapter);
try {
mTextView.setText("当前路径为:" + mCurrentParent.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
字数超了。。。。。大神尽管提问吧 展开
Mainactivity.java代码:
public class MainActivity extends Activity {
private ListView mListView;
private TextView mTextView;
private File mCurrentParent;
File[] mCurrentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list);
mTextView = (TextView) findViewById(R.id.path);
File root = new File(String.valueOf(Environment.getExternalStorageDirectory()));
if (root.exists()){
mCurrentParent = root;
mCurrentFiles = root.listFiles();
inflateListView(mCurrentFiles);
}
Button parent = (Button) findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (!mCurrentParent.getCanonicalFile().equals("/mnt/sdcard")){
mCurrentParent = mCurrentParent.getParentFile();
mCurrentFiles = mCurrentParent.listFiles();
inflateListView(mCurrentFiles);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void inflateListView(File[] files) {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(10);
for (int i = 0; i < files.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
if (files[i].isDirectory()){
listItem.put("icon", R.drawable.folder);
}else {
listItem.put("icon", R.drawable.file);
}
listItem.put("fileName", files[i].getName());
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.line, new String[]{"icon", "fileName"}, new int[]{R.id.icon, R.id.file_name});
mListView.setAdapter(simpleAdapter);
try {
mTextView.setText("当前路径为:" + mCurrentParent.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
字数超了。。。。。大神尽管提问吧 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询