user1341676
user1341676

Reputation:

Android: Multiple Activities

I've done this before, with luck, so I can't understand why I'm messing it up now.

Quite simply, start a second activity from the main activity.

In my main Activity (Test.class):

Intent s = new Intent(Test.this, Settings.class);
this.startActivity(s);

My Settings Activity (Settings.class):

public class Settings extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.settings);
    }
}

settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
        android:id="@+id/test"
        android:text="Test"
    /> 

</LinearLayout>

And here's the application part of the AndroidManifest.xml file:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.frank.test.Test"
        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.frank.test.Settings"
        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>

This just crashes. No log entries, no nothing. Not even when I surround the creating and starting of the intent/activity with a try-catch with a debug tag.

I'm blind, or deaf, I know. But I really HATE these crashes where there are no errors anywhere.

I'm starting the intent inside the main Acitvity's onCreate() method, by the way.

Update: I also tried adding the second activity to the AndroidManifest.xml class like this:

<activity android:name=".Settings"></activity>

Upvotes: 0

Views: 7903

Answers (4)

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

two launcher activity in AndroidManifest.xml ? , 2 same class Name ? here its wrong here

 <activity
        android:name="com.frank.test.Test"
        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.frank.test.Test"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

its use like and make sure your class name

  <activity
        android:name="com.frank.test.Test"
        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.frank.test.Settings"></activity>

Upvotes: 0

HitOdessit
HitOdessit

Reputation: 7256

You should specify your Settings Activity in manifest file, without specifying category.LAUNCHER for it. It should look like this:

<activity android:name=".Settings"/>

Regarding "No log entries" - it seems like you are looking into logs of your app (filtered by your app TAG). You can disable any log filtering and should see error message and stacktrace of this crash in logcat.

Upvotes: 0

Manoj Kumar
Manoj Kumar

Reputation: 1520

Change your manifest as follows:

<activity
    android:name=".Settings"/>

Upvotes: 0

waqaslam
waqaslam

Reputation: 68167

Few things you need to fix.

First, your layout:

You need to define width and height to your TextView as below:

<TextView 
    android:id="@+id/test"
    android:text="Test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" /> 

Second, you need to fix your AndroidManifest.xml. You are not properly defining both of your activities. See below:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

<!-- Test activity -->
    <activity
        android:name="com.frank.test.Test"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<!-- Settings activity -->
    <activity
        android:name="com.frank.test.Settings"
        android:label="@string/app_name" >
    </activity>
</application>

Upvotes: 3

Related Questions