0

我已成功将我的 android 应用程序连接到我的网络服务。我可以通过我的安卓应用程序发送、收集和更新我的网络服务的详细信息。但是出现了一个简单的问题,当应用程序安装在安卓手机上时。如果 Internet 网络速度较慢并且我的应用程序需要很长时间才能从 Web 服务器收集详细信息,我的 android 应用程序就会崩溃。请问我该如何解决这个问题。加载资源的类。我的代码粘贴在下面。

public class GetLgsInState extends AsyncTask<String,String,String>{
    private ProgressDialog progressdialog = null;
    Context context;
    private String state = "";

    LGModel[] localgovernments = null;



    public GetLgsInState(Context context,String state){

        this.context = context;
        this.state = state;
    }


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

        progressdialog = new ProgressDialog(context);
        progressdialog.setMessage("Loading the local government areas in state ...");
        progressdialog.show();
    }


    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        List<NameValuePair> args = new ArrayList<NameValuePair>();
        args.add(new BasicNameValuePair("id",state));
        JSONHttpClient jsonHttpClient = new JSONHttpClient();
        try{
           localgovernments = jsonHttpClient.Get(RestfulServiceUrl.GetLGInState, args, LGModel[].class);
        }
        catch(Exception e)
        {

            Log.i("collection failed","Could not collect");
        }


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (localgovernments.length > 0) {

                    for(LGModel lg : localgovernments){
                        //HashMap<String, String> mapLG = new HashMap<String, String>();
                        //mapLG.put(LGModel.LGName,lg.getName());

                        //hospitalList.add(mapLG);

                        lgspinnerAdapter.add(lg.getName());

                        lgspinnerAdapter.notifyDataSetChanged();

                    }

                }
                else
                {                       
                    //Toast.makeText(context,"No local government for this state",Toast.LENGTH_LONG);
                    errorText.setText("No Local Government entered for this state");
                }
            }
        });

        return null;
    }           


    @Override
    protected void onPostExecute(String s){
         progressdialog.dismiss();          

    }


}

我故意改变了网络服务,所以我模拟了出现网络问题时可能发生的错误

09-09 18:12:05.365: D/AndroidRuntime(380): Shutting down VM
09-09 18:12:05.365: W/dalvikvm(380): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-09 18:12:05.365: E/AndroidRuntime(380): FATAL EXCEPTION: main
09-09 18:12:05.365: E/AndroidRuntime(380): java.lang.NullPointerException
09-09 18:12:05.365: E/AndroidRuntime(380):  at com.example.medicalrobot.HospitalSearchOption$GetLgsInState$1.run(HospitalSearchOption.java:203)
09-09 18:12:05.365: E/AndroidRuntime(380):  at android.os.Handler.handleCallback(Handler.java:587)
09-09 18:12:05.365: E/AndroidRuntime(380):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 18:12:05.365: E/AndroidRuntime(380):  at android.os.Looper.loop(Looper.java:130)
09-09 18:12:05.365: E/AndroidRuntime(380):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-09 18:12:05.365: E/AndroidRuntime(380):  at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:12:05.365: E/AndroidRuntime(380):  at java.lang.reflect.Method.invoke(Method.java:507)
09-09 18:12:05.365: E/AndroidRuntime(380):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-09 18:12:05.365: E/AndroidRuntime(380):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-09 18:12:05.365: E/AndroidRuntime(380):  at dalvik.system.NativeStart.main(Native Method)
09-09 18:12:08.245: I/Process(380): Sending signal. PID: 380 SIG: 9
09-09 18:12:09.694: I/dalvikvm(409): Could not find method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.onHoverEvent, referenced from method com.actionbarsherlock.internal.widget.ActionBarContainer.onHoverEvent
09-09 18:12:09.694: W/dalvikvm(409): VFY: unable to resolve virtual method 4386: Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.onHoverEvent (Landroid/view/MotionEvent;)Z
09-09 18:12:09.694: D/dalvikvm(409): VFY: replacing opcode 0x6f at 0x0000
09-09 18:12:09.694: D/dalvikvm(409): VFY: dead code 0x0003-0004 in Lcom/actionbarsherlock/internal/widget/ActionBarContainer;.onHoverEvent (Landroid/view/MotionEvent;)Z
09-09 18:12:09.715: I/dalvikvm(409): Could not find method android.widget.FrameLayout.getAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.getAlpha
09-09 18:12:09.715: W/dalvikvm(409): VFY: unable to resolve virtual method 3088: Landroid/widget/FrameLayout;.getAlpha ()F
09-09 18:12:09.715: D/dalvikvm(409): VFY: replacing opcode 0x6f at 0x000b
4

1 回答 1

0

您的变量对象localgovernments可能正在从 Web 服务输出接收 null

检查它是否为空

if (localgovernments!=null){
 if (localgovernments.length > 0) {

                    for(LGModel lg : localgovernments){
                        //HashMap<String, String> mapLG = new HashMap<String, String>();
                        //mapLG.put(LGModel.LGName,lg.getName());

                        //hospitalList.add(mapLG);

                        lgspinnerAdapter.add(lg.getName());

                        lgspinnerAdapter.notifyDataSetChanged();

                    }
}
于 2013-09-09T18:24:32.700 回答