"App has stopped" when changing between activities - android

Hello everyone this is my first android coding and im trying to switch between activities, but i cant simply find where is my mistake.
Main here;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void start()
{
Intent k= new Intent(MainActivity.this, Try.class);
startActivity(k);
finish();
}
And My manifest is here and my second activity name is "Try";
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.ege.intent.Try"
android:label="#string/app_name" >
</activity>
</application>
Thanks.

I think;
May your class package name is wrong but there should be more information about crash.
Change android:name="com.example.ege.intent.Try"
with Try.class location, if it is same in Manifest.
Just try to Change android:name=".Try"
If you put your error or your log details, I can help you

the method "start()" is unused. use it in "onCreate()" method .
you may add a button . swtich activities by listening the click.

Where do you call start()?. Manifest seems okay but check if your package is right, anyway try would do the same, MainActivity lacks a closing semicolon }

Related

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

Launcher Activity is opening twice in my new SDK

I am recently facing a problem while creating new android application in both eclipse as well as android studio.
I am using same SDK in both eclipse and android studio.
When I create a new application and i simply run it. The launcher activity was getting loaded twice.
Means, I am getting MainActivity loading twice one on top of another.
OnCreate() method of my MainActivity also got invoked twice.
The code goes like follows
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</application>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("OnCreate Invoked");
}
}
Output
OnCreate Invoked
OnCreate Invoked
Can you please help me in resolving this issue.
Thanks in advance.
It seems you are getting multiple instance of your first activity.
Use this in manifest of 1st activity:
android:launchMode="singleTop"
If I set orientation programatically in onCreate - I can reproduce this issue.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
If needed, set activity orientation in the manifest file like this.
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How does an android program understands which class or/and activity to call first?

As i am new to android programming, I don't know how to call a certain class and/or certain activity first.
For example i have two classes viz. 1) Login.java 2) Create.java and two xml files associated with them are activity_main.xml and create_new.xml respectively.
So how can i make Login.java run first with activity_main.xml as screen?
in login java use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
in create java us this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new);
in androidmanifest
<activity
android:name=".Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and another activities
<activity android:name=".MainActivity" ></activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.your.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.your.package.activity.Login"
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="com.example.your.package.activity.Create"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
In this case, the intent-filter of LOGIN activity specifies that the intent with action of MAIN and category of LAUNCHER will be caught by it, aka it is where the application starts.
Afterwards,
public class Login extends Activity
{
//honestly I'd name this class LoginActivity and same in the XML
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
}
....
}
Also look at this example to learn how to use Fragments:
NullPointerException accessing views in onCreate()
from the intent filters you decalre in the android manifest file of your project;see the image
You declare an intent filter in the activity tag something like shown in the image for making it the first activity of your application.and if there are other activities in your app having intent filters than you have to just change the capital letter MAIN to DEFAULT in the intent filter tag
You will find AndroidManifest file like in the following image in your project

Android launch other activity before launching splash activity

I'm developing an application in android, where the app starts with a splash screen. I declared the splash activity as launcher activity in the manifest, but when I start my app, the launcher always shows up a grey activity with no content instead of the actionbar with the title of my app. The splash activity always comes up after one or two seconds.
Can anyone explain this behavior to me?
Here's my manifest:
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:logo="#drawable/navigation"
android:theme="#style/Theme.Sherlock.Light" >
<activity
android:name="de.test.basic.SplashActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity></application>
And here's the onCreate() method of the SplashActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spash_screen);
img = (ImageView) findViewById(R.id.imageView1);
img.setImageResource(R.drawable.splashscreen);
getActionbar().hide();
slideMenu.setSlidingEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,
OtherActivity.class));
overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out);
finish();
}
}, SPLASH_DELAY);
}
Thanks for any help :)
Looks like its a bug in the api!
Simply add this in your starter activity declared in your manifest:
<activity
...
android:theme="#style/Theme.NoActionBar" >
<intent-filter>
...
</intent-filter>
</activity>
And if you are using ActionBarSherlock
<activity
...
android:theme="#style/Theme.Sherlock.NoActionBar" >
<intent-filter>
...
</intent-filter>
</activity>
Your question is rather similar than this one

MapActivity launching from OnClickListener

Just started working with android and ran into a small problem. I am have a TabActivity that is loading 3 other Activity classes. This works great. I then have a button on the other Activity classes that I would like to launch a MapActivity. When I do that I keep getting a Force Close.
I googled but I cannot figure out if it is the manifest file or something else. The idea is the tabs are showing location information and you click a button to plot it on the map and get directions.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_tab);
...
Button btnMap = (Button) findViewById(R.id.btnLaunchMap);
btnMap.setOnClickListener(mMapListener);
}
private OnClickListener mMapListener = new OnClickListener() {
public void onClick(View v) {
Intent mapIntent = new Intent(getApplicationContext(),LocationMap.class);
startActivity(mapIntent);
}
};
If I launch any other activity it works but not launching the mapactivity. If I take the mapactivity class and put it in a new project it works.
My manifest
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Locations"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name=".LocationNewYork"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".LocationChicago"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name=".LocationSeattle"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name=".LocationMap"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
thougths?
<action android:name="android.intent.action.MAIN" />
inside of the mapactivity's instance ACTIVITY field in the Manifest file does it.
So if you have a MapActivity named QMap, the following code in the Manifest actually works:
<activity android:name=".QMap"><action android:name="android.intent.action.MAIN" /></activity>
Hope it helped
I had the same problem, wanted to launch map activity from an other activity over onClick-event, the problem was: errors in the MapActivity
If you are using eclipse try to run "debug as" without setting any breakpoints

Categories

Resources