0

我这里有个问题。我制作了一个应用程序,从 android 搜索数据,所有数据都保存在我的服务器上。问题是当我想按名称搜索数据时,它只是出现错误。有人可以帮帮我吗?这是我的代码。如果有任何错误我的代码,请修复它。

public class MainActivity extends ListActivity {

private ProgressDialog mProgressDialog;
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> personlist;
JSONArray person = null;

private static final String TAG_SUCCESS = "success";
private static final String TAG_PERSON = "T_Person";
private static final String TAG_PID = "ID_Person";
private static final String TAG_NAME = "First_Name_Person";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    personlist = new ArrayList<HashMap<String,String>>();

}

@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);
    SearchManager searchmanager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchview = (SearchView) menu.findItem(R.id.search).getActionView();
    searchview.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()));
    searchview.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            new LoadAllPerson().execute();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            return false;
        }
    });
    return true;
}
private class LoadAllPerson extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Please Wait");
        mProgressDialog.setMessage("Hold");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        List<NameValuePair>param = new ArrayList<NameValuePair>();
        String url_all_person = "http://172.18.0.20/search_exe.php?First_Name_Person="+TAG_NAME;
        JSONObject json = jsonParser.makeHttpRequest(url_all_person, "GET", param);
        Log.d("All Person: ", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                person = json.getJSONArray(TAG_PERSON);

                for (int i = 0; i < person.length(); i++) {
                    JSONObject c = person.getJSONObject(i);

                    String id        = c.getString(TAG_PID);
                    String firstname = c.getString(TAG_NAME);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, firstname);
                    personlist.add(map);
                }

            } else {

            }
        } catch (JSONException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        mProgressDialog.dismiss();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                ListAdapter adapter = new SimpleAdapter(MainActivity.this,personlist,R.layout.singledetail,new String[]
                        { TAG_PID,TAG_NAME},
                new int[] { R.id.textID, R.id.firstname });
                setListAdapter(adapter);
            }
        });
    }
}

}

我的logcat是这样的:

11-11 02:40:09.921: E/JSON Parser(1156): Error parsing data org.json.JSONException: End of input at character 0 of 
11-11 02:40:10.282: E/AndroidRuntime(1156): FATAL EXCEPTION: AsyncTask #1
11-11 02:40:10.282: E/AndroidRuntime(1156): java.lang.RuntimeException: An error occured while executing doInBackground()
11-11 02:40:10.282: E/AndroidRuntime(1156):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.lang.Thread.run(Thread.java:856)
11-11 02:40:10.282: E/AndroidRuntime(1156): Caused by: java.lang.NullPointerException
11-11 02:40:10.282: E/AndroidRuntime(1156):     at com.sd.search.MainActivity$LoadAllPerson.doInBackground(MainActivity.java:89)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at com.sd.search.MainActivity$LoadAllPerson.doInBackground(MainActivity.java:1)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-11 02:40:10.282: E/AndroidRuntime(1156):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-11 02:40:10.282: E/AndroidRuntime(1156):     ... 4 more
11-11 02:40:15.952: E/WindowManager(1156): Activity com.sd.search.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d68308 V.E..... R.....ID 0,0-320,162} that was originally added here
11-11 02:40:15.952: E/WindowManager(1156): android.view.WindowLeaked: Activity com.sd.search.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d68308 V.E..... R.....ID 0,0-320,162} that was originally added here
11-11 02:40:15.952: E/WindowManager(1156):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
11-11 02:40:15.952: E/WindowManager(1156):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
11-11 02:40:15.952: E/WindowManager(1156):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-11 02:40:15.952: E/WindowManager(1156):  at android.app.Dialog.show(Dialog.java:281)
11-11 02:40:15.952: E/WindowManager(1156):  at com.sd.search.MainActivity$LoadAllPerson.onPreExecute(MainActivity.java:79)
11-11 02:40:15.952: E/WindowManager(1156):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-11 02:40:15.952: E/WindowManager(1156):  at android.os.AsyncTask.execute(AsyncTask.java:534)
11-11 02:40:15.952: E/WindowManager(1156):  at com.sd.search.MainActivity$1.onQueryTextSubmit(MainActivity.java:58)
11-11 02:40:15.952: E/WindowManager(1156):  at android.widget.SearchView.onSubmitQuery(SearchView.java:1161)
11-11 02:40:15.952: E/WindowManager(1156):  at android.widget.SearchView.access$900(SearchView.java:92)
11-11 02:40:15.952: E/WindowManager(1156):  at android.widget.SearchView$8.onEditorAction(SearchView.java:1139)
11-11 02:40:15.952: E/WindowManager(1156):  at android.widget.TextView.onEditorAction(TextView.java:4117)
11-11 02:40:15.952: E/WindowManager(1156):  at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
11-11 02:40:15.952: E/WindowManager(1156):  at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
11-11 02:40:15.952: E/WindowManager(1156):  at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
11-11 02:40:15.952: E/WindowManager(1156):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 02:40:15.952: E/WindowManager(1156):  at android.os.Looper.loop(Looper.java:137)
11-11 02:40:15.952: E/WindowManager(1156):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-11 02:40:15.952: E/WindowManager(1156):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 02:40:15.952: E/WindowManager(1156):  at java.lang.reflect.Method.invoke(Method.java:511)
11-11 02:40:15.952: E/WindowManager(1156):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-11 02:40:15.952: E/WindowManager(1156):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-11 02:40:15.952: E/WindowManager(1156):  at dalvik.system.NativeStart.main(Native Method)

如果代码成功,所有数据显示为列表视图。请帮我。谢谢

4

0 回答 0