Mishra Shreyanshu
Mishra Shreyanshu

Reputation: 644

Switching between three activities in Android?

I have a scenario of three activities namely:

  1. MainActivity
  2. DerivedActivity
  3. ThirdPage

I want to navigate between 1st to second and to third.

here is my Manifest (AndroidManifest.xml)

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.implicitexplicit"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <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>
        <activity android:name=".DerivedActivity"></activity>
        <activity android:name=".ThirdPage"></activity>
    </application>

</manifest>

Now the Main Activity(MainActivity.java)

package org.example.implicitexplicit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void impliex(View v)
{
    Intent intent=new Intent(this, DerivedActivity.class);
    startActivity(intent);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

The 2nd(DerivedActivity.java)

package org.example.implicitexplicit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class DerivedActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.derived_activity);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

private void explicit(View v) {
        Intent intent = new Intent(this, ThirdPage.class);
        startActivity(intent);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId()==android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
}

and the Last Activity(ThirdPage.java)

package org.example.implicitexplicit;
import android.app.Activity;
import android.os.Bundle;
public class ThirdPage extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thirdpage_activity);
    }
}

In the DerivedActivity.java I am getting an warning

The method explicit(View) from the type DerivedActivity is never used locally

The xml files are as follows

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/eia"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    android:contentDescription="@string/imgalt"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/desc"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/clickme" 
    android:onClick="impliex"/>

</LinearLayout>

derived_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/derivedText"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/clickme"
    android:onClick="explicit"
     />

</LinearLayout>

thirdpage_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/thirdpageText"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Error :

java.lang.IllegalStateException: Could not find a method explicit(View) in the activity class org.example.implicitexplicit.DerivedActivity for onClick handler on view class android.widget.Button with id 'button1'

Upvotes: 2

Views: 1228

Answers (4)

Dheeraj_Vashist
Dheeraj_Vashist

Reputation: 79

you declare your method as private but it should be public first. second your exception shows that the name of the method is explicitURl but actually it is explicit(View)

Upvotes: 1

Prashant Jajal
Prashant Jajal

Reputation: 3627

change access modifire of that method private to public

public void explicit(View v) {
    Intent intent = new Intent(this, ThirdPage.class);
    startActivity(intent);
}

Upvotes: 2

Lazy Ninja
Lazy Ninja

Reputation: 22527

Change private void explicit to

public void explicit(View v) {
        Intent intent = new Intent(this, ThirdPage.class);
        startActivity(intent);
    }

Upvotes: 4

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

your explicit method is private you must make that method public to use as clickListener

Upvotes: 3

Related Questions