gpt4 book ai didi

android - Asynctask 无法将变量从 doInBackground 传递到 onPostExecute - 更改公共(public)变量。在执行后

转载 作者:行者123 更新时间:2023-11-29 17:54:20 25 4
gpt4 key购买 nike

我有下面的代码来处理异步任务,我正在尝试更改一个名为 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];
}
}

好的,事情就是这样。我的代码似乎一直有效,直到 doInBackground 中的 return。当它试图将 String[] 传递给 onPostExecute 时,它就崩溃了。我跟着调试,我看到所有的字符串都填满了我需要的信息。 (wart,wort,week,mth) 我就是解决不了这个问题。

这也是我的 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)

最佳答案

错误是 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);

关于android - Asynctask 无法将变量从 doInBackground 传递到 onPostExecute - 更改公共(public)变量。在执行后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20582225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com