I have an activity (DemoAppActivity) from which I am trying to launch a different activity (MainActivity) when a button is pressed. However I am getting two 开发者_运维问答errors:
1) when the app is loaded into the emulator, I see the following in the Android logs:
10-12 18:48:19.579: ERROR/dalvikvm(620): Could not find class 'com.example.android.hcgallery.MainActivity', referenced from method com.consultknapp.demoapp.DemoAppActivity$1.onClick
2) when i actually push the button that calls startActivity:
10-12 18:54:58.019: ERROR/AndroidRuntime(620): java.lang.NoClassDefFoundError: com.example.android.hcgallery.MainActivity
Here is how I am starting the activity (note: I import the class with an import statment, import com.example.android.hcgallery.MainActivity)
startActivity(new Intent(DemoAppActivity.this, MainActivity.class));
I have the MainActivity project folder in my build path in eclipse, and I even see it load the MainActivity.apk when I compile/run my DemoAppActivity. However, it bombs when I try to run it on the emulator.
What am I missing here? Do I need to jar up my MainActivity and include it in the DemoAppActivity somehow?
I was able to figure it out from another post. Basically you need to do this with the Intent before starting the activity:
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.android.hcgallery", "com.example.android.hcgallery.MainActivity"));
startActivity(i);
The setComponent tells the class loader to make it available. After adding that code, it worked!
精彩评论