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!
Related
I had built a cocos2d Android project from cocos creator. (sample application)
And I exported this android application as an android library (sample.aar)
I created another Android application (MyDemo) and imported the android library (sample.aar)
I am calling the AppActivity of (sample.aar) library from the MainActivity of (MyDemo) application on Button click
aarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("org.cocos2d.demo", "org.cocos2dx.javascript.AppActivity"));
startActivity(intent);
}
});
The result is MyDemo app crashed.
Below is the error message I get in the console
android.content.ActivityNotFoundException: Unable to find explicit activity class {org.cocos2d.demo/org.cocos2dx.javascript.AppActivity}; have you declared this activity in your AndroidManifest.xml?
Below the screenshot of the error
#But there is a trick
If I install the (sample) application on the phone first and then create an aar and import in the MyDemo application, then it works fine.
But I don't want to do like this.
I am stuck with this for couple of days.
Any help would be appreciated
I need to create a simple app without activity that just open a webpage in the browser.
This is my code:
package com.example.johnny.myapplication3;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.content.Intent;
import android.net.Uri;
/**
* Testing Fundamentals
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
}
But I get this error: Error: cannot find symbol method startActivity(Intent)
Why ?
ApplicationTestCase does not have a method startActivity. If you're creating an app, not a test case, it does not make sense to use TestCase classes at all.
Without some more info on what you want to do it's hard to help more.
"a simple app without activity that just open a webpage in the browser"
How is your app supposed to be started? If it doesn't have any activity it will not have any launcher icon. Is it supposed to start by reacting to a broadcast intent or something like that? If so, you need a receiver to handle that intent and perform the actions you need to perform. You cannot just create an app with no entry point at all, how is the system supposed to know when and how to launch it?
Assuming you do want an icon in the launcher, then you do need to register an activity. That activity could in turn finish itself and launch the browser using startActivity, but the activity must be there to act as an entry point.
I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!
I have been trying to invoke another activity from the mainactivity class with the code:
startActivity(new Intent("intent filter name here"));
However when I debug the application when it gets to this line I get a source not found message in the class file editor that says the source attachment does not contain the source for the file ClassLoader.class. There is an option to change the attached source "android.jar" but there is only one such file under the_targetAPI folder. How can i fix this?
Try this:
Intent intent = new Intent(this, secondactivity.class);
startActivity(intent);
And don't forget to add it to manifest.xml.
By upper error also try to clean or rebuild the project.
Intent from one activity to another:
Intent ii=new Intent(Firstactivity.this,Secondactivity.class);
startActivity(ii);
Import tis one :
import android.content.Intent;
Add both classes in your Manifest.xml:
<activity
android:name="com.example.task.secondActivity"
android:label="#string/app_name" />
Try this one , its may work. check the manfest.xml ,
Source not found ClassLoader.class when invoking an activity from main
this may be the reason. check it out :)
Happy coding :)
I want to call a class of another project. I added it in the build path and also declared that class in the manifest file but when I call it gives me a no class found error. I am calling it from intent.
Intent intent = new Intent(getApplicationContext(), org.coolreader.CoolReader.class);
intent.putExtra("path", adapter.getItem(position).getPath());
startActivity(intent);
If you want to launch CoolReader from your program than look at this answer:
Intent coolReaderIntent = getPackageManager().getLaunchIntentForPackage("org.coolreader.CoolReader");
coolReaderIntent.putExtra("path", adapter.getItem(position).getPath());
startActivity(coolReaderIntent);
Of course, CoolReader (program) should be installed on the device.