バスの位置情報アプリを作成しています。スピナーから選択した値を取得しようとしています。スピナーは DB から取り込まれたことに注意してください。ただし、スピナーの選択した値を取得しようとすると、アプリがクラッシュします。スピナーから選択した値を取得して、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: FATAL EXCEPTION: main Process: tosan.example.tosan.buslocation, PID: 11451 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on tosan.example.tosan.buslocation.DriversLocationUpdate$LocationUpdateClass.onPostExecute(DriversLocationUpdate.java:224) での null オブジェクト参照 tosan.example.tosan.buslocation.DriversLocationUpdate$LocationUpdateClass.onPostExecute(DriversLocationUpdate.java:196) で.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) で java.lang.reflect.Method.invoke (ネイティブ メソッド) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) で com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) でinternal.os.ZygoteInit.main(ZygoteInit.java:1410)internal.os.ZygoteInit.main(ZygoteInit.java:1410)