1

我有项目从英特尔伽利略托管的网络中获取 json 值。当我在 android 模拟器中使用异步任务时,它可以完美运行,但是当我在真实设备中尝试相同的代码时,它会抛出 NullPointerException。任何人都可以为我解释为什么以及如何解决这个问题。谢谢!

package com.example.administrator.dbin.ClassLibrary;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;

import com.example.administrator.dbin.Activity.MainActivity;
import com.example.administrator.dbin.Activity.ShowDataActivity;
import com.example.administrator.dbin.Adapter.DataAdapter;
import com.example.administrator.dbin.Adapter.RouteAdapter;
import com.example.administrator.dbin.Model.Data;
import com.example.administrator.dbin.Model.JsonDBin;
import com.example.administrator.dbin.Model.Route;
import com.example.administrator.dbin.R;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class AsyncData extends AsyncTask<Void,Void,JsonDBin>{

    private Activity context;
    private ProgressDialog dialog;
    private  View view;
    //private String httpURL = "http://192.168.78.1:8080/sendData";
    private String httpURL = "http://192.168.0.100:8080/sendData";
    private int codeProcess;

    String first_floor;
    String end_floor;
    String max_weight;

    public AsyncData(Activity context_, int codeProcess_, View view_) {
        this.context = context_;
        this.codeProcess = codeProcess_;
        this.view = view_;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        SharedPreferences sharedPreferences = context.getSharedPreferences(MainActivity.myPreferences,  Context.MODE_PRIVATE);
        first_floor = String.valueOf( sharedPreferences.getInt("floor_first", 1));
        end_floor = String.valueOf(sharedPreferences.getInt("floor_end", 1));
        max_weight = String.valueOf(sharedPreferences.getInt("floor_end", 1));

        httpURL += "/" + first_floor + "/" + end_floor + "/" + max_weight;

        dialog = new ProgressDialog(context);
        dialog.setTitle(R.string.str_progress_title);
        dialog.setMessage(context.getResources().getString(R.string.str_progress_message));
        dialog.show();
    }

    @Override
    protected JsonDBin doInBackground(Void... params) {

        try {
            URL url = new URL(httpURL);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
            JsonDBin jsonDBin = new Gson().fromJson(bufferedReader, JsonDBin.class);

            return jsonDBin;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }

            return null;
    }

    @Override
    protected void onPostExecute(JsonDBin jsonDBin) {
        super.onPostExecute(jsonDBin);

        int floor_flag;

        if (codeProcess == ShowDataActivity.CODE_DATA){

            List<Data> tmp = jsonDBin.getData();
            List<Data> data = new ArrayList<Data>();

            floor_flag = tmp.get(0).getId();
            data.add(new Data(floor_flag," ",-100));

            for (Data item : tmp){

                if (floor_flag != item.getId()){
                    floor_flag = item.getId();
                    data.add(new Data(floor_flag, "", -100));
                }
                data.add(item);
            }

            ListView listView = (ListView)view.findViewById(R.id.listViewData);
            DataAdapter adapter = new DataAdapter(context,R.layout.listview_content_row, data);
            listView.setAdapter(adapter);

        }
        else if (codeProcess == ShowDataActivity.CODE_ROUTE ){
            List<Route> tmp = jsonDBin.getRoute();
            if (!(tmp.isEmpty())){
                List<Route> route = new ArrayList<Route>();

                floor_flag = tmp.get(0).getId();
                route.add(new Route(floor_flag, " ", -100));

                for (Route item : tmp){

                    if (floor_flag != item.getId()){
                        floor_flag = item.getId();
                        route.add(new Route(floor_flag, "", -100));
                    }
                    route.add(item);
                }

                ListView listView = (ListView)view.findViewById(R.id.listViewRoute);
                RouteAdapter adapter = new RouteAdapter(context, R.layout.listview_content_row, route);
                listView.setAdapter(adapter);
            }
        }

        dialog.dismiss();
    }
}

这是我的日志猫

com.example.administrator.dbin E/AndroidRuntime: 致命异常:com.example.administrator.dbin.ClassLibrary.AsyncData.onPostExecute(AsyncData.java:94) 在 com.example.administrator.dbin 的主要 java.lang.NullPointerException。 ClassLibrary.AsyncData.onPostExecute(AsyncData.java:30) 在 android.os.AsyncTask.finish(AsyncTask.java:631) 在 android.os.AsyncTask.access$600(AsyncTask.java:177) 在 android.os.AsyncTask$ InternalHandler.handleMessage(AsyncTask.java:644) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:153) 在 android.app.ActivityThread.main(ActivityThread .java:4987) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 在 dalvik.system.NativeStart.main (本机方法)

4

0 回答 0