Android Application Crash when calling intent - java

I face a problem in android application. When I call Intent with a button the window disappear. Here is my code:
public void btn_open_click(View view) {
Intent i = new Intent(MainActivity.this, Add_User.class);
startActivity(i);
}
and here is Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.antiatlasdev.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Add_User"
android:label="#string/app_name">
</activity>
</application>
</manifest>
Any idea? Thanks in advance.

<activity
android:name="com.example.antiatlasdev.myapplication.Add_User"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"</activity>
use this

This is because you don't use the correct class name for the activity. You're using inccorrect class name:
<activity android:name="Add_User"
android:label="#string/app_name">
</ activity>
It should be using .Add_User instead of Add_User. You need to add the . to tell that your activity name is relative to your package name. Or you can also using the complete package name and the class activity name. Something like this:
<activity android:name="com.yourpackagename.Add_User"
android:label="#string/app_name">
</activity>

The name attribute
should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element.
So, if the Add_User class in in the com.example.antiatlasdev.myapplication package you can code as:
<activity
android:name=".Add_User"
android:label="#string/app_name">
</activity>
while if Add_User is in another package, for example in the com.example.antiatlasdev.anotherPackage package, then you will have to specify it like:
<activity
android:name="com.example.antiatlasdev.anotherPackage.Add_User"
android:label="#string/app_name">
</activity>

Related

How to fix the default activity not found problem?

my problem is related to Android Studio.Whenever I try to run my app I get an error that says default activity not found.Please help me out,it's really important.Thank you in advance!
Just in case,I've got 2 classes: the first one is MainActivity,the second one is CatRepository
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cats">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Try to replace <activity android:name=".ui.MainActivity"> by <activity android:name=".MainActivity">
Try putting full package name of your Activity.
You can try invalidating cache and restart.
File -> Invalidate Cache/Restart
Also after doing this, sync your project. : File-> Sync Project with Gradle Files

Android Studio Layout is not Responding

I recently have added a layout and a java file connecting it to my java code file which is taking and giving actual output by implementing an algorithm but the second layout when opened in my phone it does not react to any of the two buttons clicked on it.
Seems like an error in AndroidManifest file.
here is Manifest file code,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplicationgeneric">
<application
android:allowBackup="true"
android:label="#string/app_name2"
android:icon="#mipmap/jug_black_trasnparent"
android:roundIcon="#mipmap/jug_black_trasnparent"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Next"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".connectorclass">
<intent-filter>
<action android:name="android.intent.action.ANSWER"/>
<category android:name="android.intent.category.Calculator"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
here connectorclass.java is linked to the Layout not Responding named something xyz..
Just remove the intent filter for "connectorclass".
In AndroidManifest.xml file, you provide two intent files. That doesn't need it. Always try to add one intent-filter on your activity and this activity is your lancer activity. So remove the second intent-filter from connectorclass activity.
Then add below code on your button onClickListener
Intent intent = new Intent(MainActivity.class, connectorclass.class);
startActivity(intent);
you can remove
<intent-filter>
<action android:name="android.intent.action.ANSWER"/>
<category android:name="android.intent.category.Calculator"/>
</intent-filter>
in android manifest and can start activity using class name by default
like this
Intent intent = new Intent(this, connectorclass.class);
startActivity(intent);
try this

Android Studio android:label for a differnet activity does not display the app name [does not work]

I am trying to have a different app name for an Activity "My Profile Photo" but it does not seem to show up even when I have given android:label="My Profile Pic" to that particular activity
The Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aditya.myprofile">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="My Profile">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProfilePhoto"
android:label="Profile Pic" ></activity>
</application>
</manifest>
The XML Design:
The Android App name space is empty
Where am I going wrong?
Try this add title through java code add this code in your activity like this
getSupportActionBar().setTitle("My Profile");
I think you need to set a theme to your activity in your manifest.
<activity android:name=".ProfilePhoto"
android:theme="#style/AppTheme"
android:label="#string/profilePicture></activity>
Another solution : in your activity in onCreate you need to call :
getSupportActionBar().setTitle("Your Activity Title");

BroadcastReceiver registered in AndroidManifest not working

Current Behavior:
The messages are being broadcast, but the receiver never gets them. I know this because the print statement in the broadcaster is printed, but the print statement in the receiver is never printed. What am I missing/doing incorrectly?
Here's the snippet where I tried (multiple ways) to broadcast a message, which is inside a class called IMService that extends Service:
//NEW_FRIEND_REQUEST is a String
Log.i( "MY_TAG", "broadcasting received friend request");
Intent i = new Intent( NEW_FRIEND_REQUEST, null, null, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( NEW_FRIEND_REQUEST );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( this, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
The broadcast receiver (which is a standalone class aka not an inner class):
public class NotificationReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("MY_TAG", "friends broadcast receiver received a friend request message");
}
}
AndroidManifest.xml:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="NotificationsReceiver" >
<intent-filter>
<action android:name="IMService.NEW_FRIEND_REQUEST" />
</intent-filter>
</receiver>
<activity
android:name="activities.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="activities.Login"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name="activities.Register"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_register" >
</activity>
<activity
android:name=".HttpRequest"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_signin" >
</activity>
<activity
android:name=".Messaging"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_messaging" >
</activity>
<service android:name=".IMService" />
<activity
android:name=".FriendList"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friend_list" >
</activity>
<activity
android:name="activities.Home"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_home"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.Friends"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friends"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.FriendRequests"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friend_requests" >
</activity>
<activity
android:name="activities.PrivateChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_private_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.RandomChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_random_chat" >
</activity>
<activity
android:name="activities.Groups"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_groups" >
</activity>
<activity
android:name="activities.GroupChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_group_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.UserProfile"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_user_profile" >
</activity>
<activity
android:name=".Messages"
android:label="#string/title_activity_messages" >
</activity>
</application>
</manifest>
EDIT:
After reading Mike's comment, I tried the following 2 things (separately), but the receiver still does not receive the message:
<!-- no filter -->
<receiver android:name="NotificationReceiver" />
and:
<!-- with a filter, and I changed the value of NEW_FRIEND_REQUEST to
public static final String NEW_FRIEND_REQUEST = "new_friend_request"; -->
<receiver android:name="NotificationReceiver" >
<intent-filter>
<action android:name="new_friend_request" />
</intent-filter>
</receiver>
Here is my project's hierarchy:
Last Edit:
For anyone curious about the exact warning when I used a capital letter for the package name, here's a screen shot:
When using implicit Intents - i.e., Intents not created for a specific class - the action you're filtering for on the BroadcastReceiver must exactly match that which you use to create the Intent. In this case, the NEW_FRIEND_REQUEST String specified in the code did not match the <action> element in the manifest.
Also, when a Receiver is incorrectly listed in the manifest, firing an implicit Intent meant for it will fail silently, as the system won't be able find it. In this case, an extra s in NotificationReceiver was one problem. Another was that the Receiver is located in a subdirectory of /src, and therefore in a different package. This was corrected by changing the name listed in the manifest to include the subdirectory in the package name.
Finally, it appears that capital letters in package names - and, therefore, project directory names - can cause problems, and should be avoided.

AndroidManifest.xml with multiple application tags

I'm very new to Android programming and I've been trying to figure out why my app is force-closing on a button-click. I've narrowed it down to a few things.
One question; Is it possible to have more than one <application> tag in the manifest xml?
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dummies.android.beergoggles"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Result" android:label="#string/app_name"> </activity>
</application>
<application android:name="MyApp"
android:icon="#drawable/icon"
android:label="#string/app_name2"></application>
I've been researching, but found only a vague post about creating a new manifest file for a new application. The MyApp application is just an app for a "global variable", since I guess there's no way to do that without a new application.
Here is the code for MyApp in case it helps:
import android.app.Application;
public class MyApp extends Application{
public static int resultCount;
public int getCount(){
return resultCount;
}
public void setCount(int c){
resultCount = c;
}
}
Any help would be much appreciated.
According to documentation manifest file with only one application element is valid.
Only the <manifest> and <application> elements are required, they each
must be present and can occur only once.
What I think you want is to use your custom Application as the main Application.
So you dont add a new <application> but just specify its name to the main <application>(you need to specify its full package).
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name:"com.mypackage.MyApp"> <!-- Added the android:name -->
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Result" android:label="#string/app_name"> </activity>
</application>
See info here
Only the 'manifest' and 'application' elements are required, they each must be present and can occur only once. Most of the others can occur many times or not at all — although at least some of them must be present for the manifest to accomplish anything meaningful.

Categories

Resources