ListView Intent On Item 单击侦听器停止应用程序错误
Posted
技术标签:
【中文标题】ListView Intent On Item 单击侦听器停止应用程序错误【英文标题】:ListView Intent On Item Click Listener stop Application Error 【发布时间】:2019-11-29 18:14:51 【问题描述】:我有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));
);
【问题讨论】:
也许共享错误堆栈跟踪? 【参考方案1】:我已经检查了你的问题。您已使用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);
【讨论】:
【参考方案2】:在启动 Intent 时不应使用 getApplicationContext()。您应该将其替换为您的活动上下文。
【讨论】:
以上是关于ListView Intent On Item 单击侦听器停止应用程序错误的主要内容,如果未能解决你的问题,请参考以下文章
ListView 自己定义BaseAdapter实现单选打勾(无漏洞)
React does not recognize the `colStyle` prop on a DOM element. If you intent