Asynctask无法将变量从doInBackground传递给onPostExecute - 更改public var。在postexecute

时间:2013-12-14 10:38:19

标签: android arrays string android-asynctask

我有以下代码来处理异步任务,我正在尝试更改名为result的外部变量

    public class UP extends Fragment {
    private String[] result = new String[3];

      SumRequ connRes = new SumRequ();
  connRes.execute();

  final String bM = result[0];
  final String bT = result[1];
  final String hft = result[2];
  final String hfy = result[3];

    private class SuReq extends AsyncTask<String, String[], String[]> {

    @Override
    protected String[] doInBackground(String... params) {

        String[] connResult;

          final String neuSiparisUrlStr = getString(R.string.neu_siparis_url);
          String wart = "";
          String wort = "";
          String week = "";
          String mth = "";

        try {
              JSONTokener neuSipTokener = new JSONTokener(Sources.httpConnGet(neuSiparisUrlStr, getActivity()).toString());
                    JSONArray neuSipArray=new JSONArray(neuSipTokener);

                    for(int i=0; i<(neuSipArray.length()); i++)
                    {
                        JSONObject json_obj_neu_sip = neuSipArray.getJSONObject(i);

                        wart = json_obj_neu_sip.getString("mik");
                        wort = json_obj_neu_sip.getString("tur");
                        week = json_obj_neu_sip.getString("pt");
                        mth = json_obj_neu_sip.getString("pt2");
                    }

        } catch (Exception e) {

        }
        connResult = new String[]{wart, wort, week, mth};
        return connResult;
    }

    @Override
    protected void onPostExecute(String[] connRes) {
        super.onPostExecute(connRes);
        result[0] = connRes[0];
        result[1] = connRes[1];
        result[2] = connRes[2];
        result[3] = connRes[3];
    }
    }

好的,这就是事情。我的代码似乎正在工作,直到return doInBackground。当它试图将String[]传递给onPostExecute时,它就会崩溃。我跟着调试,我看到所有的字符串都填满了我需要的信息。 (疣,麦汁,星期,月)我无法解决问题。

这也是我的logcat:

 12-14 10:27:42.865: E/AndroidRuntime(1773): FATAL EXCEPTION: main
 12-14 10:27:42.865: E/AndroidRuntime(1773): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at com.akilli.ticaret.upfragments.UPAnasayfa.onCreateView(UPAnasayfa.java:64)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.View.measure(View.java:15848)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.View.measure(View.java:15848)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.View.measure(View.java:15848)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.View.measure(View.java:15848)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.Choreographer.doFrame(Choreographer.java:532)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.os.Handler.handleCallback(Handler.java:730)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.os.Handler.dispatchMessage(Handler.java:92)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.os.Looper.loop(Looper.java:137)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at android.app.ActivityThread.main(ActivityThread.java:5103)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at java.lang.reflect.Method.invokeNative(Native Method)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at java.lang.reflect.Method.invoke(Method.java:525)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 12-14 10:27:42.865: E/AndroidRuntime(1773):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

错误为neuSipArray.length());,并从yeniSipArray.getJSONObject(i);

获取值
for(int i=0; i<(neuSipArray.length()); i++)
                    {
              // error  ->  JSONObject json_obj_yeni_sip = yeniSipArray.getJSONObject(i);
replace to 
->          JSONObject json_obj_yeni_sip = neuSipArray.getJSONObject(i);

答案 1 :(得分:1)

好吧,这只是一个愚蠢的错误,细心的人。 String [] a = String [3]; 并不意味着: a [0],a [1],a [2],a [3] 只是: a [0],a [1]和[2]