Saturday, April 27, 2013

[android help] Trying to start an activity is causing an exception to be thrown, unsure why

java - Trying to start an activity is causing an exception to be thrown, unsure why - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















Here is the code:



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);
context.startActivity(a);
}
});
}

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

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

return convertView;
}


When I click on the ExpenseIcon button I get an exception thrown at the line 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)



I'm unsure why and not sure how to fix...



















Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.










lang-java






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...