【问题标题】:Accessing a TextView from AsyncTask (within a fragment)从 AsyncTask 访问 TextView(在片段内)
【发布时间】:2014-04-10 02:32:54
【问题描述】:

首先,我是 android 编程的新手,如果这看起来是一个愚蠢的问题,我很抱歉,我仍在努力解决这一切! 我正在尝试从我的 onPostExecute() AsyncTask 中更新片段中的 TextView。 但我正在努力从布局中获取 TextView 并对其进行更新。 我似乎能够即时创建 TextView 并对其进行更新,但我想要一个在我的 xml 布局中创建的 textview。我在这里看到了很多类似的问题,但没有一个使用片段(我正在尝试学习的片段位)。

您可以从下面的代码中看到,我最初尝试在代码中创建 TextView (tv)(已注释掉)并且效果很好。然后我尝试从布局中获取 TextView ((TextView)rootView.findViewById(R.id.tv2)),但是因为我不能将它声明为公共字符串,所以我现在无法在 onPostExecute( )。

我在这里做的事情真的很傻吗??

public static class PlaceholderFragment extends Fragment {
    String str;
    //public TextView tv;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tug_boat_main,
                container, false);

        tv = (TextView)rootView.findViewById(R.id.tv2);
        //tv = new TextView(container.getContext());
        tv.setText("hi there");
        ReadExtPortfolio rext = new ReadExtPortfolio();
        rext.execute();
        return tv;
        //return rootView;

    }

    class ReadExtPortfolio extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute() {
            tv.setText("Fetching data, please wait: ");
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            try {

                 URL url = new URL("http://mywebsite.com/andy.txt");
                 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 str = in.readLine();
                 in.close();
               } catch (IOException e) {
                  str = "error";
                }
            return null;
        }

        // Executed on the UI thread after the
        // time taking process is completed
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tv.setText("Completed the task, and the result is : " + str);
        }
    }   
}

XML 文件 (fragment_tug_boat_main) 包含...

 <TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world2" />

此外,我已经使用 stackoverflow 多年,这些年来回答问题的人为我节省了很多时间。因此,感谢您阅读本文并感谢所有回答的人。

编辑: 对不起,我应该说。如果我取消注释“public TextView tv;”然后我得到一个运行时错误:指定的孩子已经有一个父母。您必须首先在子父级上调用 removeView()。我认为调用 removeView() 听起来不正确,因为我的 TextView 上不存在 removeView

【问题讨论】:

  • 为什么评论//public TextView tv;。删除评论。您可以在 onPostExecute 和 return rootView 中将文本设置为 textview
  • 取消评论 public TextView tv; 看看..
  • 对不起,我应该说。如果我取消注释,则会收到运行时错误:指定的孩子已经有父母。您必须首先在子父级上调用 removeView()。我认为调用 removeView() 听起来不正确,因为我的 TextView 上不存在 removeView。

标签: java android android-fragments android-asynctask


【解决方案1】:

你所要做的就是取消注释 //public TextView tv;然后您将能够在 onPostExecute 方法中使用它。

【讨论】:

    猜你喜欢
    • 2017-05-19
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多