gpt4 book ai didi

java - 如何在我的适配器中启动 Activity?

转载 作者:行者123 更新时间:2023-11-29 07:37:23 25 4
gpt4 key购买 nike

如何在适配器中启动 Activity ?我在我的适配器中使用这个方法:

post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context,asabani_cat.class));
}
});

但这只适用于我的应用程序 fragment ,当我在 Activity 中调用这段代码时,应用程序突然崩溃了!

我的另一个问题是,它是在适配器中启动 Activity 的好方法吗?

我的日志:

11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: FATAL EXCEPTION: main 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: java.lang.NullPointerException 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.katibehpayam.mahdi.katibehpayam.adapter_common$7.onClick(adapter_common.java:266) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View.performClick(View.java:4377) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18044) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

最佳答案

你可以试试:

post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(),asabani_cat.class));
}
});

关于java - 如何在我的适配器中启动 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com