I got this error, and i dont know who is the problem. In some devices work de app but in other make this error.
//THIS IS THE CODE
package com.neoadn.takerenamephoto;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListaFotos extends ListActivity {
private File file, temp_file;
private List<String> myList;
String root_sd;
File imagen;
String direccionFile;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getListView().setBackgroundResource(R.drawable.fondo_p);
myList = new ArrayList<String>();
root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/Pictures/TRPHOTO" ) ;
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList ));
The line 41 is:
for( int i=0; i< list.length; i++)
This shows a listactivity of files and folders into some folders.
UPDATED:
Finally i used this to fix the problem: ( if list is not null, then load. If is null not load)
if(list != null) {
for( int i=0; i< list.length; i++) {
// do your for loop
}
}
It happens when the following statement returns null:
File list[] = file.listFiles();
That can happen when your directory "file" doens't contain any files. Because your list list is therefore null, the following statement
for( int i=0; i< list.length; i++)
crashes with a NullPointerException. I'd recommend you to check if your list variable is null before entering the for loop like this:
if(list != null) {
for( int i=0; i< list.length; i++) {
// do your for loop
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.