从DB填充微调器,检索选定的微调器值并保存到SQL DB

时间:2018-05-07 10:58:53

标签: java php android

我正在构建一个公交车位置应用程序。我试图从微调器中获取选定的值。请注意,微调器是从DB填充的,但是,当我尝试获取微调器的选定值时,我的应用程序崩溃了。我想从微调器中获取所选值并将其发送到我的数据库。

public class 
DriversLocationUpdate extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    ArrayList<String> listitems1= new ArrayList<> ();
    ArrayAdapter<String> adapter1;
    ArrayList<String> listitems2= new ArrayList<> ();
    ArrayAdapter<String> adapter2;

    Spinner busListArray, locationListArray;
    ProgressBar progressbar;
    Button btnUpd;
    String currselectedBus, logstatus, currselectedLocat;
    TextView vtxtValidation;
    TextView res1,res2;
    //String[] arrayUserinfo;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drivers_location_update);

        progressbar = (ProgressBar) findViewById(R.id.loginprogress);
        res1=(TextView) findViewById(R.id.spin1);
        res2=(TextView) findViewById(R.id.spin2);

        busListArray = (Spinner) findViewById(R.id.bus_arrays);
        adapter1 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listitems1);
        busListArray.setAdapter(adapter1);
        locationListArray = (Spinner) findViewById(R.id.location_arrays) ;
        adapter2 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listitems2);
        locationListArray.setAdapter(adapter2);

        busListArray.setOnItemSelectedListener(this);
        locationListArray.setOnItemSelectedListener(this);
        btnUpd = (Button) findViewById(R.id.btnLocUpdt);
        btnUpd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currselectedBus=res1.getText().toString();
                currselectedLocat=res2.getText().toString();
                new LocationUpdateClass().execute( currselectedBus, currselectedLocat);
            }
        });
    }

    protected void onStart(){
        super.onStart();
        backTask1 bt1 = new backTask1();
        bt1.execute();
        backTask2 bt2 = new backTask2();
        bt2.execute();
    }

    private class backTask1 extends AsyncTask<Void, Void, Void>{
        ArrayList<String> list;

        protected  void onPreExecute (){
            super.onPreExecute();
            list = new ArrayList<>();
        }

        protected  Void doInBackground(Void...params){
            InputStream is=null;
            String result ="";
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://livetipsdotnet.000webhostapp.com/fetchspinner1.php");
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String line="";
                while ((line=bufferedReader.readLine())!=null)
                {
                    result +=line;
                }
                is.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            try {
                JSONArray jsonArray = new JSONArray(result);
                for (int i=0; i<jsonArray.length(); i++)
                {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    list.add(jsonObject.getString("id"));
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
            return  null;
        }

        protected  void onPostExecute (Void result){
            listitems1.addAll(list);
            adapter1.notifyDataSetChanged();
            progressbar.setVisibility(View.GONE);
        }
    }//end of first spinner class

    private class backTask2 extends AsyncTask<Void, Void, Void>{
        ArrayList<String> list;

        protected  void onPreExecute (){
            super.onPreExecute();
            list = new ArrayList<>();
        }

        protected  Void doInBackground(Void...params){
            InputStream is=null;
            String result ="";
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://livetipsdotnet.000webhostapp.com/fetchspinner2.php");
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String line="";
                while ((line=bufferedReader.readLine())!=null)
                {
                    result +=line;
                }
                is.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            try {
                JSONArray jsonArray = new JSONArray(result);
                for (int i=0; i<jsonArray.length(); i++)
                {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    list.add(jsonObject.getString("name"));
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
            return  null;
        }

        protected  void onPostExecute (Void result){
            listitems2.addAll(list);
            adapter2.notifyDataSetChanged();
            progressbar.setVisibility(View.GONE);
        }
    }

    public class LocationUpdateClass extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... params) {
            String busID = params[0];
            String busLoc = params[1];
            String link = "http://livetipsdotnet.000webhostapp.com/buslocationupdt.php?busID="
                    + busID + "&busLoc=" + busLoc;
            try {
                OkHttpClient client = new OkHttpClient();
                RequestBody postData = new FormBody.Builder()
                        .add("type", "json")
                        .build();
                Request request = new Request.Builder()
                        .url(link)
                        .post(postData)
                        .build();
                Response response = client.newCall(request).execute();
                String result = response.body().string();
                return result;
            } catch (Exception e) {
                return e.getMessage().toString();
            }
        }

        @Override
        protected void onPostExecute(String getResult) {
            super.onPostExecute(getResult);
            DriversLocationUpdate.this.logstatus = getResult;
            vtxtValidation.setText(getResult);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View selectedItemView, int position, long id) {
        switch (parent.getId()){
            case R.id.bus_arrays:
                String list1 = parent.getItemAtPosition(position).toString();
                res1.setText(list1);
                Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + list1, Toast.LENGTH_SHORT).show();
                break;
            case R.id.location_arrays:
                String list2 = parent.getItemAtPosition(position).toString();
                res2.setText(list2);
                Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + list2, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }
}

E / AndroidRuntime:致命异常:主要                   过程:tosan.example.tosan.buslocation,PID:11451                   java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.TextView.setText(java.lang.CharSequence)&#39;在null对象引用上                       at tosan.example.tosan.buslocation.DriversLocationUpdate $ LocationUpdateClass.onPostExecute(DriversLocationUpdate.java:224)                       at tosan.example.tosan.buslocation.DriversLocationUpdate $ LocationUpdateClass.onPostExecute(DriversLocationUpdate.java:196)                       在android.os.AsyncTask.finish(AsyncTask.java:660)                       在android.os.AsyncTask.-wrap1(AsyncTask.java)                       在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:677)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:154)                       在android.app.ActivityThread.main(ActivityThread.java:6682)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 个答案:

答案 0 :(得分:0)

试试这个..

final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        String items = spinner.getSelectedItem().toString();
       //Here give a network call
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

});