gpt4 book ai didi

java - 尝试启动 Activity 导致抛出异常,不确定原因

转载 作者:行者123 更新时间:2023-12-01 14:38:20 27 4
gpt4 key购买 nike

这是代码:

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
convertView = new ExpenseIcon(context);

((ExpenseIcon) convertView)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Expense Icon Clicked");

Intent a = new Intent(context, ExpenseInput.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(a);
}
});
}

if (image == null)
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.dollar);

((ExpenseIcon)convertView).setImageBitmap(image);

return convertView;
}

当我单击 ExpenseIcon 按钮时,context.startActivity(a) 行抛出异常:

04-28 11:04:53.100: D/AndroidRuntime(24962): Shutting down VM 04-28 11:04:53.100: W/dalvikvm(24962): threadid=1: thread exiting with uncaught exception (group=0x41ebc930) 04-28 11:04:53.130: E/AndroidRuntime(24962): FATAL EXCEPTION: main 04-28 11:04:53.130: E/AndroidRuntime(24962): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.app.ContextImpl.startActivity(ContextImpl.java:944) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.app.ContextImpl.startActivity(ContextImpl.java:931) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284) 04-28 11:04:53.130: E/AndroidRuntime(24962): at com.example.mytestapp.controls.ExpenseIconGridGenerate$1.onClick(ExpenseIconGridGenerate.java:63) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.view.View.performClick(View.java:4204) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.view.View$PerformClick.run(View.java:17355) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.os.Handler.handleCallback(Handler.java:725) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.os.Handler.dispatchMessage(Handler.java:92) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.os.Looper.loop(Looper.java:137) 04-28 11:04:53.130: E/AndroidRuntime(24962): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-28 11:04:53.130: E/AndroidRuntime(24962): at java.lang.reflect.Method.invokeNative(Native Method) 04-28 11:04:53.130: E/AndroidRuntime(24962): at java.lang.reflect.Method.invoke(Method.java:511) 04-28 11:04:53.130: E/AndroidRuntime(24962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-28 11:04:53.130: E/AndroidRuntime(24962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-28 11:04:53.130: E/AndroidRuntime(24962): at dalvik.system.NativeStart.main(Native Method)

我不确定为什么,也不知道如何解决......

最佳答案

试试这个,

Intent a = new Intent(context, ExpenseInput.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(a);

关于java - 尝试启动 Activity 导致抛出异常,不确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258512/

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