gpt4 book ai didi

java - 延迟后调用方法时应用崩溃

转载 作者:行者123 更新时间:2023-12-02 06:50:38 24 4
gpt4 key购买 nike

我正在尝试在OnCreate中执行doit()方法,等待5秒钟以允许该方法的功能完全加载,然后在wait函数中调用tester()方法。
该应用程序正常运行,没有延迟,并在功能完成之前显示了“TextView”,这是文本框的输出,我想等待5秒钟,直到doit()完成加载,然后从测试仪显示texx的内容()之后等待。任何帮助,将不胜感激。
下面链接的图像是崩溃输出,该应用正常运行。

Crash output

App working without delay, but doesn't output textbox content because it hasn't loaded


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

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

new doit().execute();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
tester();
}
}, 5000);
}
public void tester() {
texx = findViewById(R.id.text1);
String text = texx.getText().toString();

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

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

SwipeFlingAdapterView flingContainer = 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) {
}
});

// Optionally add an OnItemClickListener
/*
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
@Override
public void onItemClicked(int itemPosition, Object dataObject) {

}
});

*/

}

public class doit extends AsyncTask<Void, Void, String> {
@Override
protected String 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(words);
texx.setText(words);

}
}


public void showPopup (View v){
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.dropdown_menu1);
popup.show();
}

@Override
public boolean onMenuItemClick (MenuItem item){
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}



最佳答案

在setContentView()之后在onCreate()中移动以下行:

texx = findViewById(R.id.text1);

关于java - 延迟后调用方法时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60970509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com