java - 在函数之间传递变量

标签 java android parameter-passing

我是 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);
        }
    }

 }




最佳答案

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 的文档。

关于java - 在函数之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792125/

相关文章:

java - 让计时器从日期开始计数

android - 使用 Android NDK 录音

sql - 我们可以将参数传递给 SQL 中的 View 吗?

java - 使用 Keycloak Script Mapper 聚合声明中角色的属性

java - getIntent.remove 额外的不工作

java - 在 spring boot 中使用@Profile

Android Studio 模拟器已在运行。但我在 Windows 上看不到模拟器并运行我的项目

java - 使用@ElementCollection hibernate 未保存的实例异常

powershell - 将参数从 PowerShell 传递到 FFMPEG 以进行批量视频转换的问题

javascript - 将 php 数组传输到 javascript 时出错