Stamatia Ch
Stamatia Ch

Reputation: 104

Can't go to the next Activity with an Intent

I am definitely missing something so I need an extra pair of eyes!

I have a button and when is clicked I want the NewConnection Activity to appear.

I have declared all of the Activities in AndroidManifest.xml but it doesn't work.

MainActivity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the user interface layout for this Activity
        setContentView(R.layout.activity_main);
    }

    //button addButton take you to NewConnection Activity
    public void newConnection(View view) {
        Intent intent = new Intent(MainActivity.this, NewConnection.class);
        startActivity(intent);
    }
}

NewConnection

public class NewConnection extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the user interface layout for this Activity
        setContentView(R.layout.activity_new_connection);
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tuwien.process_monitoring"
    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=".NewConnection"
            android:label="@string/app_name">          
        </activity>

    </application>

</manifest>

Upvotes: 1

Views: 3215

Answers (1)

Yurets
Yurets

Reputation: 4009

You should define your button and set listener on it. Assuming that your Button is defined in R.layout.activity_main and has id equals "btn":

Button btn = (Button)findViewById(R.id.btn); 
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
           Intent intent = new Intent(MainActivity.this, NewConnection.class);
           startActivity(intent);
        }
    });

Piotr Golinski is right. You may specify the method which gonna be called in your layout file using attribute android:onClick. Example:

<Button android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="newConnection" />

Upvotes: 1

Related Questions