You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ListView自定义Adapter中按钮跳转页面崩溃问题求助

Hey there! Let's figure out why your app is crashing when you click those buttons in your ListView adapter.

问题根源

Looking at your stack trace, the key error message is:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

This happens because you're using getApplicationContext() when initializing your adapter in MainActivity:

adapter = new CatVaccinationListAdaptor(getApplicationContext(), CatInformationList);

The application context is a global context tied to your app's entire lifecycle, not the specific Activity. When you try to start a new Activity from this context, Android doesn't know which task stack to place the new Activity into—hence the crash.

解决方案

You have two solid options here, and I'd recommend the first one for better practice:

Option 1: Use the Activity Context Instead

Replace getApplicationContext() with MainActivity.this (your Activity instance) when creating the adapter. This gives the adapter a context that's tied to your Activity's lifecycle, which is safe for starting new Activities without extra flags.

Modify your MainActivity's onCreate method:

// Replace getApplicationContext() with MainActivity.this
adapter = new CatVaccinationListAdaptor(MainActivity.this, CatInformationList);

Your existing click listener code in the adapter will work perfectly now, no other changes needed!

Option 2: Add the Required Flag to the Intent (If You Must Use App Context)

If for some reason you need to keep using the application context, you can add the FLAG_ACTIVITY_NEW_TASK flag to your Intent to tell Android where to place the new Activity:

Update your adapter's onClick method:

Button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent;
        switch (position) {
            case 0:
                intent = new Intent(CatContext, Page2cat.class);
                break;
            case 1:
                intent = new Intent(CatContext, Page3cat.class);
                break;
            case 2:
                intent = new Intent(CatContext, Page4cat.class);
                break;
            case 3:
                intent = new Intent(CatContext, Page5cat.class);
                break;
            case 4:
                intent = new Intent(CatContext, Page6cat.class);
                break;
            case 6:
                intent = new Intent(CatContext, Page7cat.class);
                break;
            case 7:
                intent = new Intent(CatContext, Page8cat.class);
                break;
            case 8:
                intent = new Intent(CatContext, Page9cat.class);
                break;
            default:
                return;
        }
        // Add the required flag
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        CatContext.startActivity(intent);
    }
});

小提醒

Sticking with the Activity context (Option 1) is better because the application context can sometimes lead to memory leaks if held onto for too long. The Activity context is properly tied to your Activity's lifecycle, so it's safer for UI-related operations like starting new Activities.

内容的提问来源于stack exchange,提问作者Ryan Ho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:26