我有ListView
.我想单击项目并更改活动。 ListView
正常工作,但是当我单击任何项目时,应用程序会停止并关闭。
ListView myListViewOfSongs;
String[] people;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListViewOfSongs = findViewById(R.id.songs);
people = new String[]{
"Mike Strong",
"Jennifer Anniston",
"Tom Bennet",
"Leander Paes",
"Liam Nesson",
"George Clooney",
"Barack Obama",
"Steve Jobs",
"Larry Page",
"Sergey Brin",
"Steve Wozniak"
};
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,people);
myListViewOfSongs.setAdapter(myAdapter);
myListViewOfSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(),PlayerActivity.class));
}
});
}
我已经检查了你的问题.您已使用getApplicationContext()
启动 Intent。请替换 getApplicationContext()
context
替换代码
startActivity(new Intent(getApplicationContext(),PlayerActivity.class));
自
Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
startActivity(intent);
或
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
启动 Intent 时不应使用 getApplicationContext((。您应该将其替换为您的活动上下文。