Can't resolve symbol 'SecondActivity' Validates resource references inside Android XML files - android

I'm new to Android Application development. I just added another activity tag in my AndroidManifest.xml file but its gives me the following message : Can't resolve symbol 'SecondActivity' Validates resource references inside Android XML files.
I want to know why is this message getting displayed?
Here is my AndroidManifest.xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aupadhyay.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/amiticon"
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=".SecondActivity">
<intent-filter>
<action android:name="anything" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And this is my SecondActivity.java file :
import android.support.v7.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
}
Picture :

Consider the following:
Remove the intent-filter inside your second activity unless you really need that.
In your second activity, override onCreate method
#override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_layout);
}
Did you create a layout for your second activity? It seems your activity is empty - although this might not be the cause of your problem. Also what is the location of your SecondActivity, a different package or in the same place as the first activity?
As shown in your code, your second activity doesn't seem to have any of that layout stuff!
I hope this helps you!

It seems your SecondActivity doesn't belongs to the package "com.aupadhyay.myfirstapp", So move it to src of the "com.aupadhyay.myfirstapp" package where ever it currently belongs to.

Related

Android: declaring particular activity as the launcher activity

I am just starting with android and trying to alter my android manifext .xml file such that a particular activity is launched first. My code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CrimeActivity">
</activity>
</application>
I have a CrimeListActivity java class which simply should display an empty activity. However, it continues to default to displaying the activity that was specified in the manifest originally. ( In this case, .CrimeActivity.
It's hard to be more specific without posting too much code so I'm hoping someone could give me a couple pointers on where to start poking around when issues like this arise.
Thanks!

Does Android need a default layout file called activity_main.xml

I'm an Android noob and I'm having difficulty finding out something basic about Android.
I currently have an activity_main.xml file.
I want to use this layout when I first start the Android emulator, using Android Studio
Does Android look for a file activity_main.xml and use it as the default layout?
Here's my AndroidManifest.xml:
<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>
</application>
So I understand that this specifies that my .MainActivity will respond to an action.MAIN intent call. What I don't know is what the action.MAIN intent call actually is, and how my activity_main.xml relates to this.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Can anyone provide an explanation or a link to a good primer that explains these basic concepts?
From manifest :
<action android:name="android.intent.action.MAIN" />
Means this activity is the entry point of the application. In your case, the MainActivity starts.
The MainActivity sets up the layout for itself - the line responsible is
setContentView(R.layout.activity_main);
Where it looks for layout file activity_main.xml. This is just the naming convention - feel free to rename the layout file and call the new one from setContentView. It's not required to be called activity_main

Android Intent problems

I'm having an issue trying to go to a specific class within my android application using the Intent.
This is my code setup:
Intent Secondscreenintent = new Intent(this, Secondscreen.class);
The error that it gives me is
android.content.ActivityNotFoundException: Unable to find explicit activity class {/project10.aventus.quiz.Secondscreen}; have you declared this activity in your AndroidManifest.xml?
After looking at my manifest I could not see any mistakes that would indicate this message.
<activity
android:name=".Main"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="Quiz">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Secondscreen"
android:label="Secondscreen" />
<activity
android:name=".Quizclass"
android:label="Quizclass"/>
These are within the tag within the tag.
But somehow I'm still getting the class not found error. I have even tried to refer the intent to the Main.class and this has given the same error that it cannot find the Main class.
Does anyone have an idea on how to fix this?
Thanks,
Shams.
In you AndroidManifest.xml you will find some thing like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="project10.aventus.quiz"
android:versionCode="1"
android:versionName="your_version_code">
...
</manifest>
package defines the root of your implementation. So you don't have to write the full path name if you define Activities, BroadCastReceiver etc...
So you do it in this way:
<activity
android:name=".Secondscreen"
android:label="Secondscreen" />
This entry means - you will find my Secondscreen.java in my root folder aka package. In your case it would be project10.aventus.quiz.
So i quess your Secondscreen.java is not there. I'm creating allways a new subpackage ui in my root folder, so my activity entry looks like this:
<activity
android:name=".ui.Secondscreen"
android:label="#string/second_screen"
android:screenOrientation="portrait" >
</activity>
Now, this entry means - you will find my Secondscreen.java here: project10.aventus.quiz.ui.Secondscreen.java

Reorganizing activities in different folders

I want to change the structure of my android application to have the activites in a subfolder called activities. So, my code structure will become
com.example.myapp.activities.MainActivity
and all the activities will reside within com.example.myapp.activities
How do I achieve this ? Also, What changes will have to be made in the manifest for this to work ? How will I access other activity classes from within other activities ?
<activity
android:name=".activities.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>
EDIT : I achieved this using dragging and dropping the activities in the folders but now I am getting this error on setContentView(R.layout.main); : main cannot be resolved or is not a
field
If you organize all activities in same packages, then you have to define the activity without package declaration in Manifest file, example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp.activities"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name=".WorldClockApplication"
<activity
android:name=".WorldClockHomeActivity"
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=".AddLocationActivity"/>
<activity android:name=".EditPreference"/>
</application>
</manifest>
Then you should call that activity inside another activity using intent. Consider I'm calling WorldClockHomeActivity from WorldClockApplication activity as follows:
Intent myIntent=new Intent(WorldClockApplication.this,WorldClockHomeActivity.class);
startActivity(myIntent);
Then do clean, refresh your project you wont get error in your project. If get error on
setContentView(R.layout.main);
that means you have problem in res/strings or res/layout or res/drawable folders not in manifest file
If you are using Eclipse create a new packet in your project and drag and drop your activities files in there. Eclipse will take care of all the necessary changes.
The changes are in your directory structure and in the manifest as you have posted in the question.
Activities can be launched (or new Intents can be sent to them) as usual through Intent(context, YourActivity.class);
I wouldn't move the activities though, rather I'd organize other Java classes in separate packages.

Android Eclipse Add New Activity

I am using Win 7.0, Eclipse and android SDK. I want to add new activity in AndroidManifest.xml Application tab as it is shown in this tutorial Android Development – Adding Screens & Button Handlers
I add an Activity name to my manifest but it does not automatically turn it into a link. e.g. I cannot click the "Name"(It is not a hyperlink as shown in the article), thus I cannot create my class.
Can You Help me? what is the problem ?
1.Go to the Androidmanifest.xml file and add the activity inside the tag
if your activity name is secondAct.
2.Create a class named secondAct.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Project1Activity"
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=".secondAct"></activity>
<activity android:name=".third"></activity>
</application>
3 . if you are using a button for going to next activity, use the following code in secondAct.java
Button fbtn=(Button)findViewById(R.id.sbtn);
fbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sec=new Intent(secondAct.this,com.asish.third.class);
startActivity(sec);
}
});
Go to the small tab underneath that says AndroidManifest.xml and shows you the XML code for it. It should look like this:
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name=".ApplicationName"
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=".AnotherActivity"></activity>
</application>
Okay, click on ADD, then select the top box that says "Create a new element at the top level, in Application" and then you should get a box with linkable NAME*.
You need to create the class first, then point to that class in your manifest... just putting the class name in the manifest is not enough. It will not automatically create it for you.
Also, it is easier to create the class first because then Eclipse will autocomplete the class name/path for you.
EDIT: AH HAH! I see what link you are talking about...
Yeah, you need to actually create the class first for that to appear.

Categories

Resources