java可滚动微调器不显示JSONAPI中的项

dgtucam1  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(249)

我有一个可滚动的微调器,我想将来自json api的数据显示到微调器中,但只要我单击微调器,应用程序就会崩溃,并出现以下错误:
java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“java.lang.string java.lang.object.tostring()”
这是我的密码:
活动\u provide \u food.xml:

<?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>

提供\u food.java:

public class Provide_food extends AppCompatActivity {

    Spinner spinner;
    String url = "*basic url here*";
    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);
            Log.e("Check",result);
            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;
    }
}

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textSize="18dp"
        android:text="Demo"/>

</LinearLayout>

请帮帮我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题