java - 在 Android 移动设备中从 JSON 获取数据会出现 fatalException main

标签 java android json

我需要帮助,我是 Android 新手,所以我需要帮助。我需要从 JSON 接收数据,我看了很多教程,但找不到答案。我需要从 JSON 链接接收数据。一旦我这样做了,我就会遇到很多错误,而且编译器也会给我很多错误。这是我的代码:

我的自定义适配器名为 CustomItemAdapter.java

package com.example.madrit.okhttp_gson_view;


import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CustomItemAdapter extends ArrayAdapter <model_item> {
    public CustomItemAdapter(Context context , List<model_item> mymodel)
    {
        super(context , 0 , mymodel);

    }

    @Nullable
    @Override
    public model_item getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        model_item model = getItem(position);
        if (convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_shop_list, parent, false);
        }
        TextView title = (TextView) convertView.findViewById(R.id.item_onthelist_title);
        TextView price = (TextView) convertView.findViewById(R.id.item_onthelist_price);
        TextView description = (TextView) convertView.findViewById(R.id.item_onthelist_description);
         title.setText(model.getName());
        price.setText(Double.toString(model.getPrice()));
        description.setText(model.getMeta_description());
        return convertView;
    }
}

我的模型项 model_item.java

package com.example.madrit.okhttp_gson_view;

import com.google.gson.annotations.SerializedName;

import java.util.List;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class model_item {
    private String id_product;
    private String id_supplier;
    private String id_manufacturer;
    private String id_category_default;
    private String id_shop_default;
    private String id_tax_rules_group;
    private String on_sale;
    private String online_only;
    private String ean13;
    private String upc;
    private String ecotax;
    private int quantity;
    private String minimal_quantity;
    private double price;
    private String wholesale_price;
    private String unity;
    private String unit_price_ratio;
    private String additional_shipping_cost;
    private String reference;
    private String supplier_reference;
    private String location;
    private String width;
    private String height;
    private String depth;
    private String weight;
    private String out_of_stock;
    private String quantity_discount;
    private String customizable;
    private String uploadable_files;
    private String text_fields;
    private String active;
    private String redirect_type;
    private String id_product_redirected;
    private String available_for_order;
    private String available_date;
    private String condition;
    private String show_price;
    private String indexed;
    private String visibility;
    private String cache_is_pack;
    private String cache_has_attachments;
    private String is_virtual;
    private String cache_default_attribute;
    private String date_add;
    private String date_upd;
    private String advanced_stock_management;
    private String pack_stock_type;
    private String id_shop;
    private int id_product_attribute;
    private String product_attribute_minimal_quantity;
    private String description;
    private String description_short;
    private String available_now;
    private String available_later;
    private String link_rewrite;
    private String meta_description;
    private String meta_keywords;
    private String meta_title;
    private String name;
    private String id_image;
    private String legend;
    private String manufacturer_name;
    private String category_default;
    @SerializedName("new")
    private String newX;
    private String orderprice;
    private int allow_oosp;
    private String category;
    private String link;
    private int attribute_price;
    private double price_tax_exc;
    private double price_without_reduction;
    private int reduction;
    private boolean specific_prices;
    private int quantity_all_versions;
    private int virtual;
    private int pack;
    private int nopackprice;
    private boolean customization_required;
    private int rate;
    private String tax_name;
    private String image_url;
    private List<?> features;
    private List<?> attachments;
    private List<?> packItems;

    public String getId_product() {
        return id_product;
    }

    public void setId_product(String id_product) {
        this.id_product = id_product;
    }

    public String getId_supplier() {
        return id_supplier;
    }

    public void setId_supplier(String id_supplier) {
        this.id_supplier = id_supplier;
    }

    public String getId_manufacturer() {
        return id_manufacturer;
    }

    public void setId_manufacturer(String id_manufacturer) {
        this.id_manufacturer = id_manufacturer;
    }

    public String getId_category_default() {
        return id_category_default;
    }

    public void setId_category_default(String id_category_default) {
        this.id_category_default = id_category_default;
    }

    public String getId_shop_default() {
        return id_shop_default;
    }

    public void setId_shop_default(String id_shop_default) {
        this.id_shop_default = id_shop_default;
    }

    public String getId_tax_rules_group() {
        return id_tax_rules_group;
    }

    public void setId_tax_rules_group(String id_tax_rules_group) {
        this.id_tax_rules_group = id_tax_rules_group;
    }

    public String getOn_sale() {
        return on_sale;
    }

    public void setOn_sale(String on_sale) {
        this.on_sale = on_sale;
    }

    public String getOnline_only() {
        return online_only;
    }

    public void setOnline_only(String online_only) {
        this.online_only = online_only;
    }

    public String getEan13() {
        return ean13;
    }

    public void setEan13(String ean13) {
        this.ean13 = ean13;
    }

    public String getUpc() {
        return upc;
    }

    public void setUpc(String upc) {
        this.upc = upc;
    }

    public String getEcotax() {
        return ecotax;
    }

    public void setEcotax(String ecotax) {
        this.ecotax = ecotax;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getMinimal_quantity() {
        return minimal_quantity;
    }

    public void setMinimal_quantity(String minimal_quantity) {
        this.minimal_quantity = minimal_quantity;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getWholesale_price() {
        return wholesale_price;
    }

    public void setWholesale_price(String wholesale_price) {
        this.wholesale_price = wholesale_price;
    }

    public String getUnity() {
        return unity;
    }

    public void setUnity(String unity) {
        this.unity = unity;
    }

    public String getUnit_price_ratio() {
        return unit_price_ratio;
    }

    public void setUnit_price_ratio(String unit_price_ratio) {
        this.unit_price_ratio = unit_price_ratio;
    }

    public String getAdditional_shipping_cost() {
        return additional_shipping_cost;
    }

    public void setAdditional_shipping_cost(String additional_shipping_cost) {
        this.additional_shipping_cost = additional_shipping_cost;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getSupplier_reference() {
        return supplier_reference;
    }

    public void setSupplier_reference(String supplier_reference) {
        this.supplier_reference = supplier_reference;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getDepth() {
        return depth;
    }

    public void setDepth(String depth) {
        this.depth = depth;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getOut_of_stock() {
        return out_of_stock;
    }

    public void setOut_of_stock(String out_of_stock) {
        this.out_of_stock = out_of_stock;
    }

    public String getQuantity_discount() {
        return quantity_discount;
    }

    public void setQuantity_discount(String quantity_discount) {
        this.quantity_discount = quantity_discount;
    }

    public String getCustomizable() {
        return customizable;
    }

    public void setCustomizable(String customizable) {
        this.customizable = customizable;
    }

    public String getUploadable_files() {
        return uploadable_files;
    }

    public void setUploadable_files(String uploadable_files) {
        this.uploadable_files = uploadable_files;
    }

    public String getText_fields() {
        return text_fields;
    }

    public void setText_fields(String text_fields) {
        this.text_fields = text_fields;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public String getRedirect_type() {
        return redirect_type;
    }

    public void setRedirect_type(String redirect_type) {
        this.redirect_type = redirect_type;
    }

    public String getId_product_redirected() {
        return id_product_redirected;
    }

    public void setId_product_redirected(String id_product_redirected) {
        this.id_product_redirected = id_product_redirected;
    }

    public String getAvailable_for_order() {
        return available_for_order;
    }

    public void setAvailable_for_order(String available_for_order) {
        this.available_for_order = available_for_order;
    }

    public String getAvailable_date() {
        return available_date;
    }

    public void setAvailable_date(String available_date) {
        this.available_date = available_date;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public String getShow_price() {
        return show_price;
    }

    public void setShow_price(String show_price) {
        this.show_price = show_price;
    }

    public String getIndexed() {
        return indexed;
    }

    public void setIndexed(String indexed) {
        this.indexed = indexed;
    }

    public String getVisibility() {
        return visibility;
    }

    public void setVisibility(String visibility) {
        this.visibility = visibility;
    }

    public String getCache_is_pack() {
        return cache_is_pack;
    }

    public void setCache_is_pack(String cache_is_pack) {
        this.cache_is_pack = cache_is_pack;
    }

    public String getCache_has_attachments() {
        return cache_has_attachments;
    }

    public void setCache_has_attachments(String cache_has_attachments) {
        this.cache_has_attachments = cache_has_attachments;
    }

    public String getIs_virtual() {
        return is_virtual;
    }

    public void setIs_virtual(String is_virtual) {
        this.is_virtual = is_virtual;
    }

    public String getCache_default_attribute() {
        return cache_default_attribute;
    }

    public void setCache_default_attribute(String cache_default_attribute) {
        this.cache_default_attribute = cache_default_attribute;
    }

    public String getDate_add() {
        return date_add;
    }

    public void setDate_add(String date_add) {
        this.date_add = date_add;
    }

    public String getDate_upd() {
        return date_upd;
    }

    public void setDate_upd(String date_upd) {
        this.date_upd = date_upd;
    }

    public String getAdvanced_stock_management() {
        return advanced_stock_management;
    }

    public void setAdvanced_stock_management(String advanced_stock_management) {
        this.advanced_stock_management = advanced_stock_management;
    }

    public String getPack_stock_type() {
        return pack_stock_type;
    }

    public void setPack_stock_type(String pack_stock_type) {
        this.pack_stock_type = pack_stock_type;
    }

    public String getId_shop() {
        return id_shop;
    }

    public void setId_shop(String id_shop) {
        this.id_shop = id_shop;
    }

    public int getId_product_attribute() {
        return id_product_attribute;
    }

    public void setId_product_attribute(int id_product_attribute) {
        this.id_product_attribute = id_product_attribute;
    }

    public String getProduct_attribute_minimal_quantity() {
        return product_attribute_minimal_quantity;
    }

    public void setProduct_attribute_minimal_quantity(String product_attribute_minimal_quantity) {
        this.product_attribute_minimal_quantity = product_attribute_minimal_quantity;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription_short() {
        return description_short;
    }

    public void setDescription_short(String description_short) {
        this.description_short = description_short;
    }

    public String getAvailable_now() {
        return available_now;
    }

    public void setAvailable_now(String available_now) {
        this.available_now = available_now;
    }

    public String getAvailable_later() {
        return available_later;
    }

    public void setAvailable_later(String available_later) {
        this.available_later = available_later;
    }

    public String getLink_rewrite() {
        return link_rewrite;
    }

    public void setLink_rewrite(String link_rewrite) {
        this.link_rewrite = link_rewrite;
    }

    public String getMeta_description() {
        return meta_description;
    }

    public void setMeta_description(String meta_description) {
        this.meta_description = meta_description;
    }

    public String getMeta_keywords() {
        return meta_keywords;
    }

    public void setMeta_keywords(String meta_keywords) {
        this.meta_keywords = meta_keywords;
    }

    public String getMeta_title() {
        return meta_title;
    }

    public void setMeta_title(String meta_title) {
        this.meta_title = meta_title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId_image() {
        return id_image;
    }

    public void setId_image(String id_image) {
        this.id_image = id_image;
    }

    public String getLegend() {
        return legend;
    }

    public void setLegend(String legend) {
        this.legend = legend;
    }

    public String getManufacturer_name() {
        return manufacturer_name;
    }

    public void setManufacturer_name(String manufacturer_name) {
        this.manufacturer_name = manufacturer_name;
    }

    public String getCategory_default() {
        return category_default;
    }

    public void setCategory_default(String category_default) {
        this.category_default = category_default;
    }

    public String getNewX() {
        return newX;
    }

    public void setNewX(String newX) {
        this.newX = newX;
    }

    public String getOrderprice() {
        return orderprice;
    }

    public void setOrderprice(String orderprice) {
        this.orderprice = orderprice;
    }

    public int getAllow_oosp() {
        return allow_oosp;
    }

    public void setAllow_oosp(int allow_oosp) {
        this.allow_oosp = allow_oosp;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public int getAttribute_price() {
        return attribute_price;
    }

    public void setAttribute_price(int attribute_price) {
        this.attribute_price = attribute_price;
    }

    public double getPrice_tax_exc() {
        return price_tax_exc;
    }

    public void setPrice_tax_exc(double price_tax_exc) {
        this.price_tax_exc = price_tax_exc;
    }

    public double getPrice_without_reduction() {
        return price_without_reduction;
    }

    public void setPrice_without_reduction(double price_without_reduction) {
        this.price_without_reduction = price_without_reduction;
    }

    public int getReduction() {
        return reduction;
    }

    public void setReduction(int reduction) {
        this.reduction = reduction;
    }

    public boolean isSpecific_prices() {
        return specific_prices;
    }

    public void setSpecific_prices(boolean specific_prices) {
        this.specific_prices = specific_prices;
    }

    public int getQuantity_all_versions() {
        return quantity_all_versions;
    }

    public void setQuantity_all_versions(int quantity_all_versions) {
        this.quantity_all_versions = quantity_all_versions;
    }

    public int getVirtual() {
        return virtual;
    }

    public void setVirtual(int virtual) {
        this.virtual = virtual;
    }

    public int getPack() {
        return pack;
    }

    public void setPack(int pack) {
        this.pack = pack;
    }

    public int getNopackprice() {
        return nopackprice;
    }

    public void setNopackprice(int nopackprice) {
        this.nopackprice = nopackprice;
    }

    public boolean isCustomization_required() {
        return customization_required;
    }

    public void setCustomization_required(boolean customization_required) {
        this.customization_required = customization_required;
    }

    public int getRate() {
        return rate;
    }

    public void setRate(int rate) {
        this.rate = rate;
    }

    public String getTax_name() {
        return tax_name;
    }

    public void setTax_name(String tax_name) {
        this.tax_name = tax_name;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }

    public List<?> getFeatures() {
        return features;
    }

    public void setFeatures(List<?> features) {
        this.features = features;
    }

    public List<?> getAttachments() {
        return attachments;
    }

    public void setAttachments(List<?> attachments) {
        this.attachments = attachments;
    }

    public List<?> getPackItems() {
        return packItems;
    }

    public void setPackItems(List<?> packItems) {
        this.packItems = packItems;
    }
}

list_main_item.java(主要 Activity ):

package com.example.madrit.okhttp_gson_view;

import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class list_main_item extends AppCompatActivity {
private  ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listview = (ListView) findViewById(R.id.item_shop_list_view);
        String url = "http://77.242.25.43:8080/girogama/modules/api/mApi/v1/categories.php?action=get_products&id_category=6&page=1&products_per_page=15";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
          client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Gson gson = new Gson();
                model_item[] model = gson.fromJson(json , model_item[].class);
                List<model_item> list = Arrays.asList(model);
                final ListAdapter myadapter = new CustomItemAdapter(list_main_item.this,list);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        listview.setAdapter(myadapter);
                        Log.i("Item_shop_activity"," te dhenat e response");
                    }
                });

            }
        });
        setContentView(R.layout.activity_list__main_item);
    }
}

我的主xml文件activity_list_main_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_item_shop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.madrit.okhttp_gson_view.list_main_item">
    <ListView
        android:id="@+id/item_shop_list_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
  </ListView>
</RelativeLayout>

错误 the error it gaved me 请我需要帮助,即使我在数据解析方面犯了任何其他愚蠢的错误!!

最佳答案

super.onCreate(savedInstanceState); 之后或在分配/访问任何 View 之前调用 setContentView(R.layout.activity_list__main_item);

关于java - 在 Android 移动设备中从 JSON 获取数据会出现 fatalException main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41862451/

相关文章:

java - 在 Spark Java 中定义要广播的对象的位置

Java 正则表达式问题 : ###tag1@@@value1###tag2@@@value2###tag3@@@value3###

javascript - 在html5中调用onclick按钮的函数

android - buildConfigField VS 类字段? resValue VS 资源

javascript - 无法读取未定义的属性 'weight' 和无法读取未定义的属性 'x' d3.js

php - Pinterest 身份验证 url 返回 404 错误?

java - 如何将 Hibernate 映射集加载为不可修改的集?

java - 创建请求并根据两个变量委托(delegate)它的最优雅的方式

Android - ellipsize ="end"不显示三个点

java - 无法从字符串创建 JSON 对象