Java 在函数之间传递变量

Java 在函数之间传递变量,java,android,parameter-passing,Java,Android,Parameter Passing,我不熟悉android studio和Java。我的应用程序使用jsoup将网站内容传递到一个数组中,每个元素都显示在可刷卡上(比如tinder) 我遇到了一个问题,当我试图将变量“words”的结果从onPostExecute()(第123行)传递到第49行的String num时,我的应用程序崩溃了。我想在onPostExcecute中获取函数的输出,并将其设置为String num,但我不确定如何执行 public class AppHome extends AppCompatActivi

我不熟悉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);
        }
    }

 }




公共类AppHome扩展AppCompatActivity实现PopupMenu.OnMenuItemClickListener{
TextView-texx;
私人ArrayList al;
专用阵列适配器阵列适配器;
私人互联网i;
字符串;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u app\u home);
texx=findviewbyd(R.id.text1);
新建doit().execute();
字符串num=单词;
字符串str[]=num.split(“,”);
final ArrayList al=新的ArrayList(Arrays.asList(str));
arrayAdapter=新的arrayAdapter(this,R.layout.item,R.id.helloText,al);
SwipeFlingDapterView flingContainer=(SwipeFlingDapterView)findViewById(R.id.frame);
registerForContextMenu(flingContainer);
flingContainer.setAdapter(阵列适配器);
flingContainer.setFlingListener(新的SwipeFlingAdapterView.onFlingListener(){
@凌驾
public void removeFirstObjectInAdapter(){
//这是从适配器(/AdapterView)中删除对象的最简单方法
Log.d(“列表”,“删除对象!”);
al.移除(0);
arrayAdapter.notifyDataSetChanged();
}
@凌驾
public void onLeftCardExit(对象dataObject){
//在左边做点什么!
//您还可以访问原始对象。
//如果您想使用它,只需强制转换它(字符串)数据对象
Toast.makeText(AppHome.this,“left”,Toast.LENGTH_SHORT.show();
}
@凌驾
RightCardExit上的公共无效(对象dataObject){
Toast.makeText(AppHome.this,“right”,Toast.LENGTH_SHORT.show();
}
@凌驾
dapterabouttoempty上的公共void(int itemsInAdapter){
//在这里询问更多数据
al.add(“XML.concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
日志d(“列表”、“通知”);
i++;
}
@凌驾
公共无效onScroll(浮动百分比){
}
});
}
公共类doit扩展了异步任务{
//字符串;
@凌驾
受保护的空位背景(空位…空位){
试一试{
Document doc=Jsoup.connect(“https://screenscrape4top40.000webhostapp.com/).get();
words=doc.text();
}catch(异常e){e.printStackTrace();}
返回null;
}
@凌驾
公共作废onPostExecute(作废避免){
super.onPostExecute(避免);
texx.setText(字);
//字符串str=(单词);
//List elephantList=Arrays.asList(str.split(“,”);
//texx.setText(elephantList.toString());
//texx.setText(elephantList);
}
}
}
公共类doit扩展异步任务{
@凌驾
受保护的空位背景(空位…空位){
字串=”;
试一试{
Document doc=Jsoup.connect(“https://screenscrape4top40.000webhostapp.com/).get();
words=doc.text();
}捕获(例外e){
e、 printStackTrace();
}
返回单词;
}
@凌驾
public void onPostExecute(字符串字){
super.onPostExecute(避免);
texx.setText(字);
//字符串str=(单词);
//List elephantList=Arrays.asList(str.split(“,”);
//texx.setText(elephantList.toString());
//texx.setText(elephantList);
}
}
现在应该没事了

问题是,您没有从
doInBackground
方法返回任何内容,因此在
onPostExecute
函数中没有得到任何内容

您可以考虑检查<代码> AcyCtase< /C> >的文档。 在doinBackground()中,返回要在PostExecute()上使用的字符串(确保它永远不会为null)

还更改了onPostExecute方法中参数的数据类型

onPostExecute(字符串Strs)

然后将其传递给晚餐。OnPostExecute()


然后你可以使用它。

你应该使用Result参数,而不是在thread中设置非易失性可变字段。如果你可以发布崩溃日志,这样我们就可以更容易地了解你的错误。非常感谢。定义异步任务子类时,请使用
AsynTask
。现在,您正在将所有三个的
Void
作为类型传递。将结果的类型更改为
String
将允许
doInBackground()
具有
String
的返回类型。它还允许
onPostExecute()
接受
字符串作为参数。下面的文章有代码示例。
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);
    }
}