【问题标题】:Passing a variable between functions在函数之间传递变量
【发布时间】:2020-07-02 15:37:25
【问题描述】:

我是 android studio 和 Java 新手。我的应用使用 jsoup 将网站的内容传递到一个数组中,其中每个元素都显示在可滑动的抽认卡上(如 tinder)

当我尝试将变量“words”的结果从 onPostExecute()(第 123 行)传递到第 49 行的 String num 时,我的应用程序崩溃了。我想获取函数的输出在 onPostExcecute 并将其设置为 String num 但我不知道该怎么做。

public class AppHome extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
    TextView texx;
    private ArrayList<String> al;
    private ArrayAdapter<String> arrayAdapter;
    private int i;
    String words;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_home);

        texx= findViewById(R.id.text1);
        new doit().execute();

        String num = words;
        String str[] = num.split(",");
        final ArrayList al = new ArrayList<String>(Arrays.asList(str));

        arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );

        SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);


        registerForContextMenu(flingContainer);


        flingContainer.setAdapter(arrayAdapter);

        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                al.remove(0);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                Toast.makeText(AppHome.this, "left", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                Toast.makeText(AppHome.this, "right", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                al.add("XML ".concat(String.valueOf(i)));
                arrayAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
            }
        });

    }
    public class doit extends AsyncTask<Void,Void,Void> {
        //String words;
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").get();
                words=doc.text();
            }catch(Exception e){e.printStackTrace();}
            return null;
        }

        @Override
        public void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            texx.setText(words);

            //String str = (words);
            //List<String> elephantList = Arrays.asList(str.split(","));
            //texx.setText(elephantList.toString());
            // texx.setText(elephantList);
        }
    }

 }




【问题讨论】:

  • 您应该使用 Result 参数而不是跨线程设置非易失性可变字段
  • 如果您能发布崩溃日志,这样我们就更容易了解您遇到的错误是什么,那就太好了。谢谢!
  • 定义异步任务子类时,使用AsynTask&lt;Params, Progress, Result&gt;。现在,您将 Void 作为所有三个的类型传递。将结果类型更改为String 将允许doInBackground() 具有String 的返回类型。它还将允许onPostExecute() 接受String 作为参数。以下文章有代码示例。 medium.com/@suragch/android-asynctasks-99e501e637e5

标签: java android parameter-passing


【解决方案1】:

在 doInBackgroud() 中返回你想在 PostExecute() 上使用的字符串(确保它永远不会变成 null)

还改变了onPostExecute方法中Parameter的数据类型

onPostExecute(String Strs)

然后将其传递给 supper.OnPostExecute()。

那么你就可以使用它了。

【讨论】:

    【解决方案2】:
    public class doit extends AsyncTask<Void, Void, String> {
        @Override
        protected Void doInBackground(Void... voids) {
            String words = "";
            try {
                Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").get();
                words = doc.text();
            } catch(Exception e) {
                e.printStackTrace();
            }
            return words;
        }
    
        @Override
        public void onPostExecute(String words) {
            super.onPostExecute(aVoid);
            texx.setText(words);
    
            //String str = (words);
            //List<String> elephantList = Arrays.asList(str.split(","));
            //texx.setText(elephantList.toString());
            // texx.setText(elephantList);
        }
    }
    

    现在应该没问题了。

    问题是,你没有从doInBackground 方法返回任何东西,因此你没有在onPostExecute 函数中得到任何东西。

    您可以考虑查看AsyncTask here 的文档。

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 2013-03-07
      • 1970-01-01
      • 2017-03-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多