在我的android应用程序中,当我使用微调器时,从API调用的数据会完美显示,但是当我使用可搜索的微调器时,它将无法正常工作。每当我单击可搜索微调器时,应用程序就会崩溃。
该应用程序崩溃并出现以下错误:
Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
这是我的代码:
activity_provide_food:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/grdnt"
tools:context=".Provide_food">
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/select_food"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginTop="250dp"
android:background="@color/bgcolor"
app:hintText="Select Item"
/>
<Button
android:id="@+id/select_food_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/select_food"
android:layout_marginTop="250dp"
android:layout_marginLeft="40dp"
android:text="Add Item"/>
<Button
android:id="@+id/check_to_cart"
android:layout_width="270dp"
android:layout_height="40dp"
android:layout_below="@+id/select_food"
android:layout_marginTop="300dp"
android:layout_marginLeft="80dp"
android:gravity="center"
android:text="Go to Cart"
android:textSize="20dp"
android:background="@drawable/custombutton"
/>
</RelativeLayout>
提供Food.java:
package com.example.helping_hands_individual;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.textclassifier.TextLinks;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.firebase.database.annotations.Nullable;
import com.google.gson.JsonArray;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Provide_food extends AppCompatActivity {
Spinner spinner;
String url = api_link;
List<Item_Model> list = new ArrayList<>();
List<String> list1 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provide_food);
spinner = (Spinner)findViewById(R.id.select_food);
new Getdata().execute();
}
class Getdata extends AsyncTask<Void,Void,String>{
String result;
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
CustomAdapter adapter = new CustomAdapter(Provide_food.this,R.layout.item, list);
spinner.setAdapter(adapter);
}
@Override
protected String doInBackground(Void... voids) {
try {
URL mainurl = new URL(url);
HttpURLConnection connection = (HttpURLConnection)mainurl.openConnection();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
result = builder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONArray array = new JSONArray(result);
for (int i=0; i<array.length(); i++){
Item_Model item_model = new Item_Model();
JSONObject object = array.getJSONObject(i);
String itemname = object.getString("Item Name");
item_model.setItemname(itemname);
list.add(item_model);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}
CustomAdapter.java:
package com.example.helping_hands_individual;
import android.content.Context;
import android.transition.Slide;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomAdapter extends ArrayAdapter {
LayoutInflater inflater;
Context context;
List<Item_Model> list;
public CustomAdapter(Context applicationContext,int resource, List<Item_Model> list){
super(applicationContext,resource);
this.context = applicationContext;
this.list = list;
inflater = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.item,null);
TextView itemname = (TextView)view.findViewById(R.id.item_names);
itemname.setText(list.get(i).itemname);
return view;
}
}