NullPointerException onTextChanged

时间:2015-06-19 07:54:44

标签: java android nullpointerexception

我正在使用http请求获取JSON格式的数据,该格式也会加载图片。我正在尝试实现onTextChanged函数,但每当我在EditText框中输入任何字母时,它总会崩溃。

/** AsyncTask to parse json data and load ListView */
        private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
            // Doing the parsing of xml data in a non-ui thread
            @Override
            protected SimpleAdapter doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return adapter;
            }

onPostExecute

protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    ChooseCategory.this.adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

来自logcat的错误:

java.lang.NullPointerException
            at ChooseCategory$ListViewLoaderTask$1.onTextChanged(ChooseCategory.java:196)
            at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
            at android.widget.TextView.handleTextChanged(TextView.java:7467)
            at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
            at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
            at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
            at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
            at android.widget.TextView.doKeyDown(TextView.java:5532)
            at android.widget.TextView.onKeyDown(TextView.java:5343)
            at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
            at android.view.View.dispatchKeyEvent(View.java:7665)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
            at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
            at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
            at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
            at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
            at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
            at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:138)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
    java.lang.NullPointerException
                at ChooseCategory$ListViewLoaderTask$1.onTextChanged

请不要将其作为副本。我看过那篇帖子,我仍然无法修复它。这就是我在这里发帖的原因。

这是ChooseCategory.java的完整代码

public class ChooseCategory extends Activity {
    ListView mListView;
    EditText searchBox;
    SimpleAdapter adapter;
    JSONObject jObject;
    StoreJSONParser countryJsonParser = new StoreJSONParser();
    // A list object to store the parsed countries list
    List<HashMap<String, Object>> countries = null;
    // Keys used in Hashmap
    String[] from = { "country","flag","details"};
    // Ids of views in listview_layout
    int[] to = { R.id.storename,R.id.icon,R.id.details};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Getting a reference to ListView of activity_main
        mListView = (ListView) findViewById(R.id.list);

        // URL to the JSON data
        String strUrl = "http://10.0.2.2/get_stores.php";

        // Creating a new non-ui thread task to download json data
        DownloadTask downloadTask = new DownloadTask();

        // Starting the download process
        downloadTask.execute(strUrl);


    }

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        try{
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        }catch(Exception e){
            Log.d("Exception:", e.toString());
        }finally{
            iStream.close();
        }

        return data;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String>{
        String data = null;
        @Override
        protected String doInBackground(String... url) {
            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                Log.d("Background Task",e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);
        }
    }

    /** AsyncTask to parse json data and load ListView */
        private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
            // Doing the parsing of xml data in a non-ui thread
            @Override
            protected SimpleAdapter doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return adapter;
            }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    ChooseCategory.this.adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

    /** AsyncTask to download and load an image in ListView */
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

        @Override
        protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

            InputStream iStream=null;
            String imgUrl = (String) hm[0].get("flag_path");
            int position = (Integer) hm[0].get("position");

            URL url;
            try {
                url = new URL(imgUrl);

                // Creating an http connection to communicate with url
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                // Connecting to url
                urlConnection.connect();

                // Reading data from url
                iStream = urlConnection.getInputStream();

                // Getting Caching directory
                File cacheDirectory = getBaseContext().getCacheDir();

                // Temporary file to store the downloaded image
                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");

                // The FileOutputStream to the temporary file
                FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                // Creating a bitmap from the downloaded inputstream
                Bitmap b = BitmapFactory.decodeStream(iStream);

                // Writing the bitmap to the temporary file as png file
                b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

                // Flush the FileOutputStream
                fOutStream.flush();

                //Close the FileOutputStream
                fOutStream.close();

                // Create a hashmap object to store image path and its position in the listview
                HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

                // Storing the path to the temporary image file
                hmBitmap.put("flag",tmpFile.getPath());

                // Storing the position of the image in the listview
                hmBitmap.put("position",position);

                // Returning the HashMap object containing the image path and position
                return hmBitmap;

            }catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            // Getting the path to the downloaded image
            String path = (String) result.get("flag");

            // Getting the position of the downloaded image
            int position = (Integer) result.get("position");

            // Getting adapter of the listview
            SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();

            // Getting the hashmap object at the specified position of the listview
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);

            // Overwriting the existing path in the adapter
            hm.put("flag",path);

            // Noticing listview about the dataset changes
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

3 个答案:

答案 0 :(得分:0)

你有一个名为adapter的类变量吗? 似乎ChooseCategory.this.adapter不是局部变量 adapter = new SimpleAdapter(getBaseContext(),countries,R.layout.restaurant_list,from,to);

你应该命名类变量mAdapter。

答案 1 :(得分:0)

按如下方式更改Asyn类:

private class ListViewLoaderTask extends AsyncTask<String, Void, String>{  **//Change SimpleAdapter to String**
            // Doing the parsing of xml data in a non-ui thread
            @Override **//Change SimpleAdapter to String**
            protected String doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return null;
            }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override **//Change SimpleAdapter to String**
        protected void onPostExecute(String result) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(ChooseCategory.this, countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                   adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

答案 2 :(得分:0)

我认为您的edittext初始化存在问题。

searchBox = (EditText)findViewById(R.id.searchBox);

将此行从onPostexcute()移至OnCreate()