"Hello world" android project, straight from book, yielding ActivityNotFoundException sometimes - java

I'm just learning to work with Android through a textbook (Learn Android App Development by Jackson). At the moment, I have a MainActivity class. I'm adding Intents to the menu of this activity to launch one of four other activities depending on which option. All activities are in the same package, and all have been declared in the AndroidManifest.XML file.
I am determining which activity to run using a Switch case as follows:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
System.out.println("Add");
Intent intent_add = new Intent(this, NewPlanet.class);
this.startActivity(intent_add);
break;
case R.id.menu_attack:
System.out.println("Attack");
Intent intent_attack = new Intent(this, AttackActivity.class);
this.startActivity(intent_attack);
break;
case R.id.menu_config:
System.out.println("Config");
Intent intent_config = new Intent(this, ConfigActivity.class);
this.startActivity(intent_config);
break;
case R.id.menu_travel:
System.out.println("Travel");
Intent intent_travel = new Intent(this, TravelActivity.class);
this.startActivity(intent_travel);
break;
default:
return super.onOptionsItemSelected(item);
}
System.out.println("Outside switch.");
return true;
}
The problem, however, is that this only works when pressing the "Add" menu button, which successfully launches the NewPlanet activity and displays it.
All of the others, however, produce an ActivityNotFoundException and force the program to crash (same result on various combinations of virtual devices, as well as on my physical Galaxy Note II device).
I've done everything I can think of to try to fix this to no avail. As far as I know, the code is identical to that presented in the book, but the book has begun to move onto the next sections while my project is not yet working yet.
I have LogCat output if anybody wants to see that, but any help or advice would be greatly appreciated. I've Googled the issue which did not help much.
EDIT: As requested, here is my Manifest: (I attached it as a high-res images since I'm having trouble with the editor right now)
http://i.imgur.com/c7wJ8bM.png
And here is the relevant LogCat output:
07-03 10:42:53.954: E/AndroidRuntime(29754): FATAL EXCEPTION: main
07-03 10:42:53.954: E/AndroidRuntime(29754): android.content.ActivityNotFoundException: Unable to find explicit activity class {chapter.two.hello_world/chapter.two.hello_world.ConfigActivity}; have you declared this activity in your AndroidManifest.xml?
FINAL EDIT:
I've solved this problem thanks to user E. Odebugg: I was referring to my activities in the manifest by incorrect names (ConfigPlanet instead of ConfigActivity). I simply did not notice the difference. A silly mistake, but it has been fixed now. Thank you everybody for your help!

There might be something wrong with your manifest. Maybe you copied and pasted the same attributes for all Activities and hence have multiple Launcher IntentFilter Categories. Can you post both, the stack trace and the Manifest?

Clearly, the screen-shot you have shared shows that you have declared some ConfigPlanet activity, while in your switch-case u are calling ConfigActivity.
Replace .ConfigPlanet from your AndroidManifest.xml with .ConfigActivity, making sure that you do have a ConfigActivity.java file in your proper package.

You don't have the ConfigActivity declared in your manifest.
Somehow you have an entry there for ConfigPlanet which is not the same. So that's the cause.

Make sure that all your activities are declared in your AndroidManifest.xml.
<manifest ... >
<application ... >
<activity android:name="your.packagename.AttackActivity" />
<activity android:name="your.packagename.ConfigActivity" />
<activity android:name="your.packagename.TravelActivity" />
...
</application ... >
...
</manifest >
For details, see http://developer.android.com/guide/components/activities.html

Related

Does Android store the activities you've surfed?

I'm creating an app for a project. The thing is that I'm using BottomNavigationView to navigate through activites. However, there is an issue here, when I press the "Back Button", the app goes through every single page I've surfed even though I've been to a same tab multiple times.
What I want to reach is something similar to what Instagram has implemented: Going through a same tab just once despite having navigated through it previously.
Watch the gif to see what I mean. https://gyazo.com/a7f536dbe1b204923ea790db50e8a88e
I execute this code all the times I touch an item from BottomNavigationView.
case R.id.bnHome:
Intent intentHome = new Intent(context,HomeActivity.class);
context.startActivity(intentHome);
callingActivity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
break;
The current situation: when I start to close: Search -> Profile -> Search -> Profile -> Home -> exit
What I'm expecting: when I start to close: Search -> Profile -> Home -> exit
This behavior could be reach with Fragments. Fragment it is like View but with own lifecycle similarly to Activity, they was created for reuse and decoupling UI. Here is an example how to build exactly what you need.
Firstly you need to use Fragment for more readable and maintainable navigation structure.
Secondly ,
Make your activity to single task
<activity
android:name=".MyActivity"
android:launchMode="singleTask" >
</activity>
Add finish() after your startActivity calls expect from Home.
case R.id.bnHome:
Intent intentHome = new Intent(context,HomeActivity.class);
context.startActivity(intentHome);
callingActivity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
finish();
break;
It can be work but if you choose to use fragment you do not need to initialize bottom bar or common other views or actions for every page.

What might be preventing the HomeAsUp icon being displayed

I've been working on an app with multiple ActionBar activities, and from the outset the HomeAsUp button has been visible and working on all of the activities selected from the MainActivity. Today I noticed that it is no longer there - but I can't figure out what I have changed that would affect it.
The most significant change in recent days is that I migrated the project from Eclipse to Android Studio because I was having problems with Eclipse frequently corrupting my project files and requiring a complete reinstall.
This is a sample of the code in the main activity which creates the new activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent=null;
int id = item.getItemId();
switch (id) {
case R.id.action_list:
intent = new Intent(this, StudyListActivity.class);
break;
// etc
}
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Here is the declaration of the intent in the manifest
<activity
android:name="com.example.app.StudyListActivity"
android:label="#string/activity_list"
android:parentActivityName="com.example.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
since it stopped working I have also tried putting
getSupportActionBar().setHomeButtonEnabled(true);
in the onCreate method of the called activity (as per Display back button on action bar) but that makes no difference.
So I'm wondering if there is anything that got stripped out in my migration from eclipse which is preventing it showing (or something else I could have changed which might inadvertently have disabled it)
you should enable it via setDisplayOptions
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
You could also try
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In the activity you want the HomeAsUp icon in

Cannot find symbol class Intent

I am new in android development and coding java and xml.
but I was following this tutorial:
http://www.steventrigg.com/activities-and-the-action-bar-create-an-alarm-clock-in-android-tutorial-part-1/#comment-296
then I had this error when using Intent. The word "Intent" under switch became red and there is an error "cannot find symbol class Intent"
Can someone explain to me what is going on and how to solve this?
This is the last part of my code under AlarmListActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_add_new_alarm: {
Intent intent = new Intent(this,
AlarmDetailsActivity.class);
startActivity(intent);
break;
}
}
return super.onOptionsItemSelected(item);
}
Look at your AlarmListActivity again and check the import statements at the top and make sure it includes the line:
import android.content.Intent;
If you intend to use any pre-existing classes that aren't part of the java.lang package, you generally have to import those classes. An Android Intent, for example, is a pre-built class, written by the Android development team, that allows for notification of other apps/activities. If you want to use Intent, you'd then have to import the package containing Intent.
When you write new Intent(), the compiler sees that you're requesting the construction of a new object, but because that object is not found in the java.lang package, it needs to know where to look for a blueprint to build that object. The import statement is the location of that blueprint.
I took a look at the tutorial and in the manner of experienced programmers, the author seems to have glossed over a few basic, but nonetheless, important things, such as the import statements that make his sample code work.
I had a same problem which I just resolved, by understanding how Android Studio indexes files, As you know Building an Android App is quite complicated process. So Android studio has some internal references which it keeps getting updated on change of every file that you have created.
I arrived at this post while searching for the solution,
This is how I got this problem
I usually wont create an activity under the main project package, I create sub packages to organize files as per the design pattern that I use, for eg If my APP name is com.example.testingaravind then inside that I usually create packages such as activites, services, models, managers etc ... So today I just created an activity first and then moved that activity into activites package via Android Studio, I started facing the same issue what you have described, Below was my source code
public class BootstrapActivity extends ActionBarActivity {
private static final String TAG = "BootstrapActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bootstrap);
}
public void startServiceOnClickHandler(View view) {
Intent intent = new Intent(BootstrapActivity.this , AnalyzerService.class);
startService(intent);
}
}
In the method startServiceOnClickHandler it was showing an error saying,
"Cannot resolve constructor Intent" I searched a lot in google and found that
When I move a file from one package to other package, my manifest file wont get updated, in the manifest we mention the activity name and its package path in my case it should be
android:name=".activities.BootstrapActivity"
But it was
android:name=".BootstrapActivity"
Because of this, Android studio was unaware that a class called BootstrapActivity exists inside the activities folder,
This seems to be a bug in the way how android studio works. Android Studio has to update manifestfile when I move the activity class file from one package to another package.
I am posting this to help others who might arrive at this post with the similar usecase.
Check name in manifest file in activity tag specify correct package for example your
<activity
android:name="Your-Package.MainActivity"
android:exported="true" />
In second case If you are using kotlin class inside java class, after configuring kotlin in project can solve the project...

calling another activity android manifest xml

I have two sperate applications and I want to call start an activity from the second application in the first, here is my code to do so :
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
H.startActivity(intent1);
And in the android manifest of the project I have this code, I have the line :
<activity android:name=".one">
</activity>
But I keep getting a runtime error, logcat says :
"Unable to find explicit activity class
{org.two.three.application/org.two.three.application.one}; have you
declared this activity in your AndroidManifest.xml?"
Can anyone see my error? The only thing I can think of is my package of the first activity is org.two.three.Class while the second is org.two.three.application.SecondClass. Does this matter?
Thanks in advance
At first try removing those code that you are adding
**
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
**
Then add following code into an action method like onClick
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
You just need to make your activity publicly available. To do that just add
android:exported="true"
to the <activity> tag in your manifest.
Normally, activities are not available to other components that are outside of the package. This is the standard default behaviour. But, of course, you can make them available if you want to.

Android onConfigurationChanged() is not being called in Activity

I realize that there are a couple other posts on this topic, however the solutions for those posts are not working for me.
Basically, I want to cease my Activity from restarting upon a device orientation change. To do this, I have modified the activity in the manifest file:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden"></activity>
and I have overridden onConfigurationChanged() in my Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("IN onConfigurationChanged()");
}
However, the activity is still restarting upon an orientation change, and the onConfigurationChanged() method is not being called.
Does anyone know why this may be happening?
You should use 13 API and set this configuration in your activity's part of the manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
It works fine. At all Android version.
Change your manifest to following
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden|screenSize"></activity>
and refer this link for detailed explanation orientation issue
The only thing that worked was using getLastNonConfigurationInstance(). http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()
You should not use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); method call any where in your application this will avoid calling onConfigChanged() method.
if you define configchanges=orientation in your manifest then the activity won't restart but instead onConfigurationChanged will be call as you have currently have it implemented. First try to log that with the log class Log (this is the proper way to log things in android don't use System out for this its considered a bad practice) and before super but that is just a 1% chance it will fix what is happening to you.
The second case is that you have the current activity nested in a tabHost for example or Activity Group. if your activity has a parent activity then configuration change needs to be added in that one and the callback will happen there.
If that is the case and you want to forward the result or also do something in the child then you need to get a reference to the child in the parent and call a method on it for the changes.
if you have a fragment then you need this also:
void setRetainInstance(boolean retain)
Control whether a fragment
instance is retained across Activity re-creation (such as from a
configuration change).
i ran into this and setting it to 'true' fixed it.
I used this, and it helped:
package="com.s2dio.evallet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
Modefiy your onConfigurationChanged method to the following
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

Categories