Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 转换结果java.lang.NullPointerException时出错:lock==null_Android_Json_Eclipse_Android Asynctask_Nullpointerexception - Fatal编程技术网

Android 转换结果java.lang.NullPointerException时出错:lock==null

Android 转换结果java.lang.NullPointerException时出错:lock==null,android,json,eclipse,android-asynctask,nullpointerexception,Android,Json,Eclipse,Android Asynctask,Nullpointerexception,我是开发android应用程序的新手。我已经阅读了很多关于我所问问题的相关帖子,但是帖子中的提示或解决方案并没有解决我的问题。(寻找解决方案已经一周了,真的需要帮助才能继续我的项目)非常感谢 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicN

我是开发android应用程序的新手。我已经阅读了很多关于我所问问题的相关帖子,但是帖子中的提示或解决方案并没有解决我的问题。(寻找解决方案已经一周了,真的需要帮助才能继续我的项目)非常感谢


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.aroma.slidingmenu.listener.JSONParser;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ResultListFragment extends ListFragment {
    
    TextView resultView;

    
public ResultListFragment(){}
    
    //progress dialog
    private ProgressDialog pDialog;
    
    //creating JSON Parser object
    JSONParser jParser = new JSONParser();
    
    ArrayList<HashMap<String,String>> customerList;
    
    //url to get the customer list
    private static String url_search="http://192.168.1.3:80/test/getAllCustomers.php";
    
    //JSON Node names
    private static final String TAG_SUCCESS="success";
    private static final String TAG_CUSTOMER="customers";
    private static final String TAG_FNAME="FirstName";
    private static final String TAG_LNAME="LastName";
    private static final String TAG_AGE="Age";
    private static final String TAG_MOBILE="Mobile";
    
    //product JSONArray
    JSONArray customers=null;
    //search key value
    public String searchKey;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.activity_searchresult_list, container, false);
            
        Toast.makeText(getActivity(),"Search result in listview",Toast.LENGTH_LONG).show();
        
        Intent intent = getActivity().getIntent();
        searchKey = intent.getStringExtra("message");
        //Toast.makeText(getActivity(), searchKey, Toast.LENGTH_LONG).show();
        
        //hshmap for listview
        customerList= new ArrayList<HashMap<String,String>>();
        
        
        //Loading customer in background thread
        new LoadCustomer().execute();

        return rootView;       
    }
    
    @Override 
        public void onViewCreated (View view, Bundle savedInstanceState) {
            
        ListView lv =getListView();
        
        lv.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String iid=((TextView)view.findViewById(R.id.FirstName)).getText().toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
                
            }
        });
    }
    
    /**
        * Background Async Task to load customers by making HTTP request
        * */
    class LoadCustomer extends AsyncTask<String, String, String>{
        /**
            * Before starting background thread show progress dialog
            * */
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog=new ProgressDialog(getActivity());  //pDialog=new ProgressDialog(ResultListFragment.this);
            pDialog.setMessage("Loading customers. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        
        /**
            * getting customers url
            * **/
        protected String doInBackground(String... args){
            //Building Parameters
            List<NameValuePair> params= new ArrayList<NameValuePair>();
            //value captured from previous intent
            params.add(new BasicNameValuePair("FirstName", searchKey));
            //getting JSON string from url
            JSONObject json = jParser.makeHttpRequest(url_search, "GET", params);
            //check your log cat for JSON response
            Log.d("Search customer", json.toString());
            
            try{
                //checking for SUCCESS TAG
                int success=json.getInt(TAG_SUCCESS);
                
                if(success==1){
                    //product found
                    //Getting array of products
                    customers=json.getJSONArray(TAG_CUSTOMER);
                    
                    //looping through all products
                    for(int i=0;i<customers.length();i++){
                        JSONObject c=customers.getJSONObject(i);
                        
                        //storing each json item in variable
                        String fname=c.getString(TAG_FNAME);
                        String lname=c.getString(TAG_LNAME);
                        String age=c.getString(TAG_AGE);
                        String mobile=c.getString(TAG_MOBILE);
                        
                        //creating new HashMap
                        HashMap<String, String> map=new HashMap<String, String>();
                        
                        //adding each child node to HashMap key =>value
                        map.put(TAG_FNAME, fname);
                        map.put(TAG_LNAME, lname);
                        map.put(TAG_AGE, age);
                        map.put(TAG_MOBILE, mobile);
                        
                        //adding HashList to ArrayList
                        customerList.add(map);
                    }
                }else{
                        //no customer found
                        //do sth
                    Handler handler =  new Handler(getActivity().getMainLooper());
                    handler.post( new Runnable(){
                        public void run(){
                            Toast.makeText(getActivity(),"no customer found" ,Toast.LENGTH_LONG).show(); 
                        }
                    });
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
                
                //return "success";
                return null;
            }
        
        /**
            * After completing background task dismiss the progress dialog
            * **/
        protected void onPostExecute(String file_url){
            //dimiss the dialog after getting the related customer
            pDialog.dismiss();
            getActivity().runOnUiThread(new Runnable(){
                public void run(){
                    /**
                        * updating parsed JSON data into ListView
                        * **/
                    ListAdapter adapter =new SimpleAdapter(
                            getActivity(), customerList, 
                            R.layout.list_item, new String[]{ TAG_FNAME, TAG_LNAME, TAG_AGE, TAG_MOBILE},
                            new int[]{R.id.FirstName,R.id.LastName,R.id.Age,R.id.Mobile});
                        //updating listview
                        setListAdapter(adapter);
                }
            });

        }

    }
    
    
}

我从Logcat保存的错误:

02-12 17:17:50.599: E/Buffer Error(13636): Error converting result java.lang.NullPointerException: lock == null 02-12 17:17:50.599: E/JSON Parser(13636): Error parsing data org.json.JSONException: End of input at character 0 of 02-12 17:17:50.604: E/AndroidRuntime(13636): FATAL EXCEPTION: AsyncTask #4 02-12 17:17:50.604: E/AndroidRuntime(13636): java.lang.RuntimeException: An error occured while executing doInBackground() 02-12 17:17:50.604: E/AndroidRuntime(13636): at android.os.AsyncTask$3.done(AsyncTask.java:299) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 02-12 17:17:50.604: E/AndroidRuntime(13636): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.lang.Thread.run(Thread.java:838) 02-12 17:17:50.604: E/AndroidRuntime(13636): Caused by: java.lang.NullPointerException 02-12 17:17:50.604: E/AndroidRuntime(13636): at com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:136) 02-12 17:17:50.604: E/AndroidRuntime(13636): at com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:1) 02-12 17:17:50.604: E/AndroidRuntime(13636): at android.os.AsyncTask$2.call(AsyncTask.java:287) 02-12 17:17:50.604: E/AndroidRuntime(13636): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 02-12 17:17:50.604: E/AndroidRuntime(13636): ... 4 more 02-12 17:17:50.599:E/缓冲区错误(13636):转换结果java.lang.NullPointerException时出错:lock==null 02-12 17:17:50.599:E/JSON解析器(13636):解析数据org.JSON.JSONException时出错:输入在的字符0处结束 02-12 17:17:50.604:E/AndroidRuntime(13636):致命异常:AsyncTask#4 02-12 17:17:50.604:E/AndroidRuntime(13636):java.lang.RuntimeException:执行doInBackground()时出错 02-12 17:17:50.604:E/AndroidRuntime(13636):在android.os.AsyncTask$3.done(AsyncTask.java:299) 02-12 17:17:50.604:E/AndroidRuntime(13636):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 02-12 17:17:50.604:E/AndroidRuntime(13636):在java.util.concurrent.FutureTask.setException(FutureTask.java:219) 02-12 17:17:50.604:E/AndroidRuntime(13636):在java.util.concurrent.FutureTask.run(FutureTask.java:239) 02-12 17:17:50.604:E/AndroidRuntime(13636):在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 02-12 17:17:50.604:E/AndroidRuntime(13636):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 02-12 17:17:50.604:E/AndroidRuntime(13636):位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 02-12 17:17:50.604:E/AndroidRuntime(13636):在java.lang.Thread.run(Thread.java:838)处 02-12 17:17:50.604:E/AndroidRuntime(13636):由以下原因引起:java.lang.NullPointerException 02-12 17:17:50.604:E/AndroidRuntime(13636):在com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:136) 02-12 17:17:50.604:E/AndroidRuntime(13636):位于com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:1) 02-12 17:17:50.604:E/AndroidRuntime(13636):在android.os.AsyncTask$2.call(AsyncTask.java:287) 02-12 17:17:50.604:E/AndroidRuntime(13636):在java.util.concurrent.FutureTask.run(FutureTask.java:234) 02-12 17:17:50.604:E/AndroidRuntime(13636):。。。4更多
尝试在创建的活动中调用asynctask

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_searchresult_list, container, false);         

    return rootView;       
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Toast.makeText(getActivity(),"Search result in listview",Toast.LENGTH_LONG).show();

    Intent intent = getActivity().getIntent();
    searchKey = intent.getStringExtra("message");
    //Toast.makeText(getActivity(), searchKey, Toast.LENGTH_LONG).show();

    //hshmap for listview
    customerList= new ArrayList>();


    //Loading customer in background thread
    new LoadCustomer().execute();

}

您还可以检查为什么在ResultListFragment中的行号
136
中获取NPE?行号136是Log.d(“搜索客户:,json.toString());如果NPE的意思是,json.toString()得到了空值?是
json
is
null
检查你得到了什么
json=sb.toString()您好,我试图在onActivityCreated中调用asynctask,但遇到了相同的错误。再次您好。。。呵呵。。我发现了一个问题,我不知道为什么会发生这种情况。我尝试用上面的代码在GenyMoon中调试我的应用程序,并获得了预期的输出。但当它与android设备一起使用时,缓冲区错误和解析器错误被捕获。我必须重新启动电脑,用我的设备重新运行应用程序,问题解决了。但这个问题似乎不一致。我更喜欢用真正的android设备和真正的url进行测试。对于emulator或localhost wamp mysql服务器,我也有类似的问题
02-12 17:17:50.599: E/Buffer Error(13636): Error converting result java.lang.NullPointerException: lock == null
02-12 17:17:50.599: E/JSON Parser(13636): Error parsing data org.json.JSONException: End of input at character 0 of 
02-12 17:17:50.604: E/AndroidRuntime(13636): FATAL EXCEPTION: AsyncTask #4
02-12 17:17:50.604: E/AndroidRuntime(13636): java.lang.RuntimeException: An error occured while executing doInBackground()
02-12 17:17:50.604: E/AndroidRuntime(13636):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.lang.Thread.run(Thread.java:838)
02-12 17:17:50.604: E/AndroidRuntime(13636): Caused by: java.lang.NullPointerException
02-12 17:17:50.604: E/AndroidRuntime(13636):    at com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:136)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at com.example.aroma.slidingmenu.ResultListFragment$LoadCustomer.doInBackground(ResultListFragment.java:1)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-12 17:17:50.604: E/AndroidRuntime(13636):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-12 17:17:50.604: E/AndroidRuntime(13636):    ... 4 more
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_searchresult_list, container, false);         

    return rootView;       
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Toast.makeText(getActivity(),"Search result in listview",Toast.LENGTH_LONG).show();

    Intent intent = getActivity().getIntent();
    searchKey = intent.getStringExtra("message");
    //Toast.makeText(getActivity(), searchKey, Toast.LENGTH_LONG).show();

    //hshmap for listview
    customerList= new ArrayList>();


    //Loading customer in background thread
    new LoadCustomer().execute();

}