使用.setOnItemClickListener打开新意图

时间:2013-11-16 10:50:42

标签: android android-intent

private void readShops() {
    RetailerAdapter retailersAdapter = new RetailerAdapter(
            getApplicationContext(), retailers);
    final ListView listViewRetailers = (ListView) findViewById(R.id.listViewRetailers);
    listViewRetailers.setAdapter(retailersAdapter);

    listViewRetailers
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parentView,
                        View childView, int position, long id) {
                    Intent intentNews = new Intent(this, RetailerActivity.class);
                    intentNews.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intentNews);
                }
            });
}

因为Intent(这是RetailerActivity.class)而导致错误;

Intent(null,RetailerActivity.class);修复它,然后应用程序崩溃。

任何人都可以帮助我吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

替换

Intent intentNews = new Intent(this, RetailerActivity.class);

Intent intentNews = new Intent(YourActivityName.this, RetailerActivity.class);

答案 1 :(得分:0)

而不是在你的Intent中使用YourActivityName.this或getApplicationContext();