如何从asyntask返回settext

时间:2014-08-23 13:52:46

标签: android textview settext

我正在尝试设置从asyntask返回的textview的值,但它会产生错误。 这是我的logcat

08-23 19:03:18.359: E/AndroidRuntime(26496): FATAL EXCEPTION: main
08-23 19:03:18.359: E/AndroidRuntime(26496): android.content.res.Resources$NotFoundException: String resource ID #0x1
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.content.res.Resources.getText(Resources.java:247)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.widget.TextView.setText(TextView.java:3539)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at com.shajeel.daily_monitoring.fragments.MainFragment$1.onTaskComplete(MainFragment.java:125)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at com.shajeel.daily_monitoring.threads.GetJobOrdersTask.onPostExecute(GetJobOrdersTask.java:145)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at com.shajeel.daily_monitoring.threads.GetJobOrdersTask.onPostExecute(GetJobOrdersTask.java:1)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.os.AsyncTask.finish(AsyncTask.java:602)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.os.Looper.loop(Looper.java:137)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at java.lang.reflect.Method.invoke(Method.java:511)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-23 19:03:18.359: E/AndroidRuntime(26496):    at dalvik.system.NativeStart.main(Native Method)

我如何设置tvJobOrder的值。 这是我的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment_layout, container,
                false);

        mSeekBar = (SeekBar) v.findViewById(R.id.mSeekBar);
        mSpinner = (Spinner) v.findViewById(R.id.mSpinner);

        tvJobOrder = (TextView) v.
                findViewById(R.id.tvMainJobOrder);
        setRetainInstance(true);

        id = SharedPreferencesUtils.getString(getActivity(), AppConstants.USER_ID_KEY, "");
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState == null) {
            new GetJobOrdersTask(new GetJobOrdersTask.TaskCompleteListner() {


                @Override
                public void onTaskComplete(List<GetJobOrderModel> list) {
                    // TODO Auto-generated method stub
                    for (int i = 0; i < list.size(); i++) {

                        mJobOrderList.add(list.get(i).getTitle());
                        mJobOrderIdsList.add(list.get(i).getJobID());
                        mJobOrderIdsList.add(list.get(i).getStatus());
                    }

                    mAdapter.notifyDataSetChanged();
                    SharedPreferencesUtils.saveString(getActivity(),
                            AppConstants.CURRENT_JOB_KEY, mJobOrderIdsList.get(0));
                    tvJobOrder.setText(list.size());
                }
            }, SharedPreferencesUtils.getString(
                    MainFragment.this.getActivity(), AppConstants.USER_ID_KEY,
                    "")).execute();

        mSeekBar.setOnSeekBarChangeListener(this);

        mAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.custom_spinner_item, mJobOrderList);
        mAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        mSpinner.setAdapter(mAdapter);      
        mSpinner.setOnItemSelectedListener(this);
    }
}

1 个答案:

答案 0 :(得分:2)

你正在调用方法

 tvJobOrder.setText(list.size());
带有int参数的

,它接受字符串资源ID。在你的情况下,它显然不存在,这不是你想要实际做的。

要解决此问题,请执行以下操作:

tvJobOrder.setText("" + list.size());