【问题标题】:ListView Intent On Item Click Listener stop Application ErrorListView Intent On Item 单击侦听器停止应用程序错误
【发布时间】: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));
        }
    });

}

【问题讨论】:

  • 也许共享错误堆栈跟踪?

标签: java android android-layout listview android-intent


【解决方案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()。您应该将其替换为您的活动上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多