0

i am trying to make an android app that searches the phones files and lists it under the search box as the user types in real time. please help in correcting the code, am getting a null pointer exception. all the resources are in place.

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ListActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;

public class MainActivity extends ListActivity implements TextWatcher {
    File[] myfiles;
    FilenameFilter filter;
    TextWatcher watcher;
    String query;
    static File file;
    EditText eT;
    ListView list;
    ArrayList<String> myList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProgressBar progressBar = new ProgressBar(this);
        progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   
                LayoutParams.WRAP_CONTENT));

        progressBar.setIndeterminate(true);
        list.setEmptyView(progressBar);

        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        root.addView(progressBar);

        list = (ListView) findViewById(android.R.id.list);

        File file = new File("Environment.getExternalStorageDirectory().getPath()");
        myfiles = file.listFiles(filter);

        Log.d("tag", "files passed");
        eT = (EditText) findViewById(R.id.editText1);
        }








    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }








    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        query = ((EditText) s).getText().toString();

                filter = new FilenameFilter() {


                    @Override
                    public boolean accept(File dir, String filename) {
                        String lcaseName = filename.toLowerCase();
                        if(lcaseName.contains(query)) {
                            return true;
                        }
                        else {
                        return false;
                        }
                    }

                };
                list.setAdapter(new ArrayAdapter<String>(this, 
                                android.R.layout.simple_list_item_1, myList));
                myList = new ArrayList<String>();

                for(int i =0; i < myfiles.length; i++) {
                  myList.add(myfiles[i].getName());

                }





    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }








    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub






    }

}
  • 3
    1. See the exception stacktrace in logcat. 2. Find the corresponding line in your code. 3. Figure out what could be the reason, fix it and test to see if the problem is gone. 4. If that doesn't help, provide information from steps 1 and 2 in the question so others can help. – laalto Jan 25 '14 at 11:02
  • please am new to android and i don't know how to use logcat properly. any help will be appreciated. thanks – user3234870 Jan 25 '14 at 11:37

2 Answers2

2
list.setEmptyView(progressBar);

 // ...

list = (ListView) findViewById(android.R.id.list);

You're using list before it's initialized.

Debugging process pulled from question comment:

  1. See the exception stacktrace in logcat.
  2. Find the corresponding line in your code.
  3. Figure out what could be the reason. Test your theory by fixing the code and testing again.

When posting questions about crashes in your code, always include information from steps 1 and 2.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Thanks it removed the error. But now i have a new problem: it doesn't display any result. please how do i edit the code? guess am doing it wrong altogether – user3234870 Jan 25 '14 at 11:33
1

variable list is not initialized. But after fixing this you will get NullPointerException again becouse you are using filter that is also null.

myfiles = file.listFiles(filter);
Armen
  • 255
  • 1
  • 2
  • 12
  • thanks but the filter isn't null. i probably defined it inappropriately at the text changed callback method. Any suggestions on how to make it really work – user3234870 Jan 25 '14 at 11:35