private Intent intent = null; void initialize() { inventoryButton = findViewById(R.id.inventoryButton); findButton = findViewById(R.id.findButton); settingButton = findViewById(R.id.settingButton); exitButton = findViewById(R.id.exitButton); languageButton = findViewById(R.id.languageButton); inventoryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Intent intent = new Intent(HomeActivity.this, HomeActivity.class); if (ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext())) { setContentView(R.layout.activity_main); CustomToast.showLoad(HomeActivity.this, getString(R.string.load)); startActivity(intent); // 读取 Excel 文件 List<String> data = readExcelFile(); listView = findViewById(R.id.rv_list); // 将数据显示在 ListView 中 ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); } } });有什么问题,怎么解决
时间: 2023-07-15 22:15:57 浏览: 107
Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码
这段代码的问题在于,`intent` 变量没有被初始化,而且在 `onClick` 方法中没有被赋值,因此会导致启动 `Activity` 失败。你可以将 `intent` 的初始化放在 `initialize` 方法中,并在 `onClick` 方法中赋值,修改后的代码如下:
```
private Intent intent = null;
void initialize() {
inventoryButton = findViewById(R.id.inventoryButton);
findButton = findViewById(R.id.findButton);
settingButton = findViewById(R.id.settingButton);
exitButton = findViewById(R.id.exitButton);
languageButton = findViewById(R.id.languageButton);
// 初始化 intent
intent = new Intent(HomeActivity.this, HomeActivity.class);
inventoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext())) {
setContentView(R.layout.activity_main);
CustomToast.showLoad(HomeActivity.this, getString(R.string.load));
// 在 onClick 方法中赋值
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// 读取 Excel 文件
List<String> data = readExcelFile();
listView = findViewById(R.id.rv_list);
// 将数据显示在 ListView 中
ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
});
}
```
另外,你需要确保 `readExcelFile()` 方法已经正确实现,否则也可能会导致启动 `Activity` 失败。
阅读全文