android日志打印错误

时间:2014-04-16 15:28:21

标签: android json android-asynctask

我正在运行Android应用程序,该应用程序从网站下载信息并在屏幕上打印包含该信息的列表。

然而,当它试图加载列表时,应用程序崩溃,我得到以下logcat:

  04-15 23:32:41.946: I/Choreographer(2069): Skipped 40 frames!  The application may be doing too much work on its main thread.
04-15 23:32:50.038: I/Choreographer(2069): Skipped 33 frames!  The application may be doing too much work on its main thread.
04-15 23:32:52.096: I/DO IN BACKGROUD(2069): {"succeed":true,"user_id":362}
04-15 23:32:52.096: I/MESSAGE(2069): {"succeed":true,"user_id":362}
04-15 23:32:52.146: I/put Extra user id(2069): 362
04-15 23:32:56.256: I/dalvikvm-heap(2069): Grow heap (frag case) to 3.554MB for 635812-byte allocation
04-15 23:32:59.027: I/Connection(2069): true
04-15 23:32:59.257: I/Choreographer(2069): Skipped 79 frames!  The application may be doing too much work on its main thread.
04-15 23:33:00.456: I/Choreographer(2069): Skipped 297 frames!  The application may be doing too much work on its main thread.
04-15 23:33:01.447: I/Choreographer(2069): Skipped 229 frames!  The application may be doing too much work on its main thread.
04-15 23:33:03.138: I/Choreographer(2069): Skipped 184 frames!  The application may be doing too much work on its main thread.
04-15 23:33:03.856: I/Choreographer(2069): Skipped 186 frames!  The application may be doing too much work on its main thread.
04-15 23:33:04.349: I/DO IN BACKGROUD(2069): {  "supermercado": "La Salle - Curs Android II",   "productos" : [     {           "id" : 1,           "fabricante" : "Nestlé",            "nombre" : "NESQUIK",           "precio" : 12.85,           "stock" : 10        },      {           "id" : 2,           "fabricante" : "Coca-cola",         "nombre" : "Coca-cola",         "precio" : 3.21,            "stock" : 150       },      {           "id" : 3,           "fabricante" : "Borges",            "nombre" : "Aceite girasol 1L",         "precio" : 1.88,            "stock" : 0     },      {           "id" : 4,           "fabricante" : "Serpis",            "nombre" : "Aceitunas rellenas de anchoa 160g",         "precio" : 1.39,            "stock" : 1523      },      {           "id" : 5,           "fabricante" : "Carretilla",            "nombre" : "Pimientos piquillo ajillo 290g",            "precio" : 2.25,            "stock" : 14        },      {           "id" : 6,           "fabricante" : "Orlando",           "nombre" : "Tomate frito con aceite de oliva virgen brik 390g",         "precio" : 0.95,            "stock" : 0     },      {           "id" : 7,           "fabricante" : "Kellogg's",         "nombre" : "Cereales Special K chocolate 300g",         "precio" : 2.39,            "stock" : 152       },      {           "id" : 8,           "fabricante" : "Lu",            "nombre" : "Galletas rellenas mini Principe 160g",          "precio" : 1.59,            "stock" : 29        },      {           "id" : 9,           "fabricante" : "Pascual",           "nombre" : "Leche sin lactosa desnatada brik 1l",           "precio" : 1.49,            "stock" : 1596      },      {           "id" : 10,          "fabricante" : "Danone",            "nombre" : "Danet vainilla 4x125g",         "precio" : 1.90,            "stock" : 856       },      {           "id" : 11,          "fabricante" : "Hornimans",         "nombre" : "Infusueños 20 sobres",          "precio" : 2.35,            "stock" : 598       }   ]}
04-15 23:33:04.516: I/Choreographer(2069): Skipped 169 frames!  The application may be doing too much work on its main thread.
04-15 23:33:04.848: I/Choreographer(2069): Skipped 59 frames!  The application may be doing too much work on its main thread.
04-15 23:33:05.188: I/On Post EXECUTE(2069): {  "supermercado": "La Salle - Curs Android II",   "productos" : [     {           "id" : 1,           "fabricante" : "Nestlé",            "nombre" : "NESQUIK",           "precio" : 12.85,           "stock" : 10        },      {           "id" : 2,           "fabricante" : "Coca-cola",         "nombre" : "Coca-cola",         "precio" : 3.21,            "stock" : 150       },      {           "id" : 3,           "fabricante" : "Borges",            "nombre" : "Aceite girasol 1L",         "precio" : 1.88,            "stock" : 0     },      {           "id" : 4,           "fabricante" : "Serpis",            "nombre" : "Aceitunas rellenas de anchoa 160g",         "precio" : 1.39,            "stock" : 1523      },      {           "id" : 5,           "fabricante" : "Carretilla",            "nombre" : "Pimientos piquillo ajillo 290g",            "precio" : 2.25,            "stock" : 14        },      {           "id" : 6,           "fabricante" : "Orlando",           "nombre" : "Tomate frito con aceite de oliva virgen brik 390g",         "precio" : 0.95,            "stock" : 0     },      {           "id" : 7,           "fabricante" : "Kellogg's",         "nombre" : "Cereales Special K chocolate 300g",         "precio" : 2.39,            "stock" : 152       },      {           "id" : 8,           "fabricante" : "Lu",            "nombre" : "Galletas rellenas mini Principe 160g",          "precio" : 1.59,            "stock" : 29        },      {           "id" : 9,           "fabricante" : "Pascual",           "nombre" : "Leche sin lactosa desnatada brik 1l",           "precio" : 1.49,            "stock" : 1596      },      {           "id" : 10,          "fabricante" : "Danone",            "nombre" : "Danet vainilla 4x125g",         "precio" : 1.90,            "stock" : 856       },      {           "id" : 11,          "fabricante" : "Hornimans",         "nombre" : "Infusueños 20 sobres",          "precio" : 2.35,            "stock" : 598       }   ]}
04-15 23:33:05.486: I/Write STORAGE(2069): {    "supermercado": "La Salle - Curs Android II",   "productos" : [     {           "id" : 1,           "fabricante" : "Nestlé",            "nombre" : "NESQUIK",           "precio" : 12.85,           "stock" : 10        },      {           "id" : 2,           "fabricante" : "Coca-cola",         "nombre" : "Coca-cola",         "precio" : 3.21,            "stock" : 150       },      {           "id" : 3,           "fabricante" : "Borges",            "nombre" : "Aceite girasol 1L",         "precio" : 1.88,            "stock" : 0     },      {           "id" : 4,           "fabricante" : "Serpis",            "nombre" : "Aceitunas rellenas de anchoa 160g",         "precio" : 1.39,            "stock" : 1523      },      {           "id" : 5,           "fabricante" : "Carretilla",            "nombre" : "Pimientos piquillo ajillo 290g",            "precio" : 2.25,            "stock" : 14        },      {           "id" : 6,           "fabricante" : "Orlando",           "nombre" : "Tomate frito con aceite de oliva virgen brik 390g",         "precio" : 0.95,            "stock" : 0     },      {           "id" : 7,           "fabricante" : "Kellogg's",         "nombre" : "Cereales Special K chocolate 300g",         "precio" : 2.39,            "stock" : 152       },      {           "id" : 8,           "fabricante" : "Lu",            "nombre" : "Galletas rellenas mini Principe 160g",          "precio" : 1.59,            "stock" : 29        },      {           "id" : 9,           "fabricante" : "Pascual",           "nombre" : "Leche sin lactosa desnatada brik 1l",           "precio" : 1.49,            "stock" : 1596      },      {           "id" : 10,          "fabricante" : "Danone",            "nombre" : "Danet vainilla 4x125g",         "precio" : 1.90,            "stock" : 856       },      {           "id" : 11,          "fabricante" : "Hornimans",         "nombre" : "Infusueños 20 sobres",          "precio" : 2.35,            "stock" : 598       }   ]}
04-15 23:33:07.027: W/dalvikvm(2069): threadid=1: thread exiting with uncaught exception (group=0x41465700)
04-15 23:33:07.266: E/AndroidRuntime(2069): FATAL EXCEPTION: main
04-15 23:33:07.266: E/AndroidRuntime(2069): java.lang.NullPointerException: println needs a message
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.util.Log.println_native(Native Method)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.util.Log.i(Log.java:159)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.example.shop.Tenda.refreshListByJson(Tenda.java:305)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.example.shop.Tenda.access$1(Tenda.java:302)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.example.shop.Tenda$LongAsyncTask.onPostExecute(Tenda.java:259)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.example.shop.Tenda$LongAsyncTask.onPostExecute(Tenda.java:1)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.os.AsyncTask.finish(AsyncTask.java:631)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.os.Looper.loop(Looper.java:137)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at java.lang.reflect.Method.invoke(Method.java:525)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-15 23:33:07.266: E/AndroidRuntime(2069):     at dalvik.system.NativeStart.main(Native Method)
04-15 23:33:13.366: I/Process(2069): Sending signal. PID: 2069 SIG: 9
04-15 23:33:20.867: I/Choreographer(2098): Skipped 60 frames!  The application may be doing too much work on its main thread.

以下是发生崩溃的活动:

Tenda.java

package com.example.shop;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.example.shop.SuperAdapter;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

public class Tenda extends Activity implements OnItemClickListener, LocationListener {

    ProgressDialog lDialog;
    String json_string;
    SuperAdapter adapter_super;
    int user_id;
    private LocationManager locationManager;
    public String provider;
    private String  lattitude , longitude; 

    /*************************************************/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tenda);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        init_conf();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tenda, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_update:
                updateList();
                return true;
            case R.id.menu_search:
                alert_search();
                return true;   
            case R.id.menu_cancel:
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

        TextView stock = (TextView) view.findViewById(R.id.list_product_stock);
        int num_stock = Integer.parseInt(stock.getText().toString()) ; 

        if(num_stock!=0){
            ArrayList<Super> productes=adapter_super.getItems_producte();
            Super current_product= productes.get(position);

            alert_buy(num_stock,current_product);
        }
        else
        {
            Toast.makeText(this, "OUT OF STOCK", Toast.LENGTH_LONG).show();
        }

    }

    private void init_conf(){

        Bundle extras = getIntent().getExtras();
        user_id = extras.getInt("user_id");

        Toast.makeText(this, "user id"+Integer.toString(user_id), Toast.LENGTH_LONG).show();

        ListView lsv_producto = (ListView)findViewById(R.id.list_productes);
        adapter_super = new SuperAdapter(Tenda.this,null);
        lsv_producto.setAdapter(adapter_super);
        lsv_producto.setOnItemClickListener(this);
        updateList();

    }

    private void updateList(){
        loading();
        String url = "http://www.v2msoft.com/clientes/lasalle/curs-android/products.php?user_id="+user_id;

        boolean is_con=is_connected();

        if(is_con){
            Log.i("Connection", "true");
            AsyncTask(url);
            Toast.makeText(this, "connected internet", Toast.LENGTH_LONG).show();
        }
        else{
            json_string=storeRead();
            refreshListByJson(json_string);
            Toast.makeText(this, "not connected internet", Toast.LENGTH_LONG).show();
        }

    }

    private void search(String text){
        loading();
        String url="http://www.v2msoft.com/clientes/lasalle/curs-android/search.php?user_id="+user_id+"&q="+text;

        boolean is_con=is_connected();
        if(is_con){
            Log.i("search URL", url);
            Toast.makeText(this, url, Toast.LENGTH_LONG).show();
            searchAsyncTask(url);
        }
        else{
            Toast.makeText(this, "NO CONNECTION", Toast.LENGTH_LONG).show();
        }


    }

private void buy(Super producte, int value){

        int product_id=  producte.getId();

        String url="http://www.v2msoft.com/clientes/lasalle/curs-android/buy.php?user_id="+user_id+
                    "&product_id="+product_id+
                    "&items="+value+
                    "&lat="+1+
                    "&long="+1;

        Toast.makeText(this, url, Toast.LENGTH_LONG).show();

    }

    private void AsyncTask(String url){
        LongAsyncTask task = new LongAsyncTask();
        task.execute(url);
    }

    private void searchAsyncTask(String url){
        SearchAsyncTask task = new SearchAsyncTask();
        task.execute(url);
    }

    public void alert_search(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");

        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String m_Text = input.getText().toString();
                Toast.makeText(getApplicationContext(), m_Text, Toast.LENGTH_LONG).show();
                search(m_Text);
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();

    }

    public void alert_buy(int max_value,final Super producte){

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("BUY");

        final NumberPicker number= new NumberPicker(this);
        number.setMaxValue(max_value);
        number.setMinValue(0);
        builder.setView(number);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int value= number.getValue();
                Toast.makeText(getApplicationContext(), value+"", Toast.LENGTH_LONG).show();
                buy(producte,value);
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();

    }

    /*************************ASYNC TASK************************/

    public class LongAsyncTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... url) {
            String url_result= conect(url[0]);
            Log.i("DO IN BACKGROUND", url_result);
            return url_result;

        }
       protected void onPostExecute(String url_result) {
           Log.i("On Post EXECUTE", url_result);
           json_string= url_result;
           storeWrite(json_string);
           refreshListByJson(json_string);//line 259
       }

    }

        public class SearchAsyncTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... url) {
            String url_result= conect(url[0]);
            Log.i("Search result", url_result);
            return url_result;

        }

       protected void onPostExecute(String url_result) {
           Log.i("On Post EXECUTE SEARCH", url_result);
           json_string= url_result;
           Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
           refreshListByJson(json_string);
       }

    }

    public class BuyAsyncTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... url) {
            String url_result= conect(url[0]);
            Log.i("buy result", url_result);
            return url_result;

        }
       protected void onPostExecute(String url_result) {
           Log.i("On Post EXECUTE SEARCH", url_result);
           json_string= url_result;
           Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
       }

    }

    /*************************************************/

    private void refreshListByJson(String json){//line 302
        lDialog.dismiss();
       Store store=Store.newStore(json);
           Log.i("store-nom", store.getStore());//line 305
           Log.i("store-producte", store.getProductos().toString());
           adapter_super.setItems_producto(store.getProductos());    
    }

    private void storeWrite(String data){
        String FILENAME = "json_store";

        FileOutputStream fos;
        try {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(data.getBytes());
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.i("Write STORAGE", data);

    }

    private String storeRead(){
        String FILENAME = "json_store";

        FileInputStream  fis;
        StringBuilder fileContent = new StringBuilder();
        try {
            fis= openFileInput (FILENAME);
            BufferedReader br= new BufferedReader(new InputStreamReader(fis));
            String line;

            while((line = br.readLine())!= null)
            {
                fileContent.append(line);
            }
            br.close();
            fis.close();
            Log.i("READ STORAGE", fileContent.toString());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return fileContent.toString();
    }

    public String conect(String url_string){

        HttpURLConnection con = null;
        BufferedReader reader= null;

        try{
            URL url = new URL(url_string);
            con = (HttpURLConnection)url.openConnection();

            reader= new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line ="";
            StringBuffer responseBuffer = new StringBuffer();

            while((line=reader.readLine())!=null)
            {
                responseBuffer.append(line);
            }

            return responseBuffer.toString(); 
        }
        catch(Exception ex){
            Log.e(getClass().getName(), ex.getMessage(),ex);
            return  null;
        }

    }

    public boolean is_connected(){
        ConnectivityManager conMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
          if (i == null)
            return false;
          if (!i.isConnected())
            return false;
          if (!i.isAvailable())
            return false;
          return true;
    }

    private void loading(){
         lDialog = new ProgressDialog(this);
         lDialog.setMessage("Loading...");
         lDialog.setCancelable(false);
         lDialog.show();
    }


/*** Location ***/

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
int lat = (int) (arg0.getLatitude());
int lng = (int) (arg0.getLongitude());
lattitude = "Lattitude: "+ lat ;
longitude = "Longitude: "+ lng; 
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
////TODO Auto-generated method stub

}               

}

班级:

SuperAdapter

package com.example.shop;

import java.util.ArrayList;

import com.example.shop.Super;
import com.example.shop.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class SuperAdapter extends BaseAdapter {


    public ArrayList<Super> getItems_producte() {
        return items_producte;
    }

    private Activity activity;
    private ArrayList<Super> items_producte;

    public SuperAdapter(Activity activity, ArrayList<Super> items_producte){
        this.activity = activity;
        this.items_producte= items_producte;
    }


    @Override
    public int getCount() {
        if(items_producte==null){
            return 0;
        }
        else{
            return items_producte.size();
        }   }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items_producte.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup Parent) {
        // TODO Auto-generated method stub
        View view = contentView;

        if(view==null){
            LayoutInflater inflate =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflate.inflate(R.layout.info_product, null,false);
        }

        Super item_producto = items_producte.get(position);

        TextView id = (TextView)view.findViewById(R.id.list_product_id);
        TextView fabricant = (TextView)view.findViewById(R.id.list_product_fabricant);
        TextView nom = (TextView)view.findViewById(R.id.list_product_nom);
        TextView preu = (TextView)view.findViewById(R.id.list_product_preu);
        TextView stock = (TextView)view.findViewById(R.id.list_product_stock);

        id.setText(Integer.toString(item_producto.getId()));
        fabricant.setText(item_producto.getFabricant());
        nom.setText(item_producto.getNom());
        preu.setText(Float.toString(item_producto.getPreu()));
        stock.setText(Integer.toString(item_producto.getStock()));

        fabricant.setTextColor(activity.getResources().getColor(R.color.fabricant));
        nom.setTextColor(activity.getResources().getColor(R.color.fabricant));
        preu.setTextColor(activity.getResources().getColor(R.color.fabricant));
        stock.setTextColor(activity.getResources().getColor(R.color.fabricant));

        if(position%2==0){
            view.setBackgroundColor(activity.getResources().getColor(R.color.files_parelles));

        }
        else{
            view.setBackgroundColor(activity.getResources().getColor(R.color.files_imparelles));
        }

        /*STOKE COLOR*/
        if(item_producto.getStock()<=0){
            view.setBackgroundColor(activity.getResources().getColor(R.color.stock_null_bg));

            fabricant.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
            nom.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
            preu.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
            stock.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));

        }


        return view;
    }


    public void setItems_producto(ArrayList<Super> items_producto) {
        this.items_producte = items_producto;
        this.notifyDataSetChanged();
    }



}

**LongAsyncTask**

    package com.example.shop;
    import android.os.AsyncTask;

    public class LongAsyncTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... arg0) {

            return null;
        }


       protected void onPostExecute(String result) {

       }

    }

Store.java

    package com.example.shop;

import java.util.ArrayList;
import com.google.gson.Gson;

public class Store {

    private String store;
    private ArrayList<Super> productes;

    public String getStore() {
        return store;
    }
    public void setSupermercado(String store) {
        this.store = store;
    }
    public ArrayList<Super> getProductos() {
        return productes;
    }
    public void setProductes(ArrayList<Super> productes) {
        this.productes = productes;
    }

    static Store newStore(String json_string){

        Gson gson= new Gson();

        Store store = gson.fromJson(json_string,Store.class);

        return store;

    }


}

Super.java

package com.example.shop;

public class Super {

    private int id;
    private String fabricant;
    private String nom;
    private float preu;
    private int stock;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFabricant() {
        return fabricant;
    }
    public void setFabricant(String fabricant) {
        this.fabricant = fabricant;
    }
    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }
    public float getPreu() {
        return preu;
    }
    public void setPreu(float preu) {
        this.preu = preu;
    }
    public int getStock() {
        return stock;
    }
    public void setStock(int stock) {
        this.stock = stock;
    }

}

LongAsyncTask.java

package com.example.shop;
import android.os.AsyncTask;

public class LongAsyncTask extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... arg0) {

        return null;
    }


   protected void onPostExecute(String result) {

   }

}

有人能帮助我理解错误是什么吗?我建立了这个应用程序基于一个非常相似的应用程序,它确实工作,但当它试图在屏幕上打印列表时我的崩溃。

我也检查了所有类,我没有看到从一个到另一个的函数调用有任何问题,这样:我真的被困在这个。

非常感谢你。

1 个答案:

答案 0 :(得分:2)

根据Logcat您获得了NullPointerException

com.example.shop.Tenda.refreshListByJson(Tenda.java:305)

在这一行:

Log.i("store-nom", store.getStore());//line 305

这意味着store.getStore()会返回null。您必须向null提供非String Log

您可以尝试这样打印:

Log.i("store-nom", "" + store.getStore());

或者只是确保store.getStore()不是null