suji
suji

Reputation: 1470

Cannot navigate to another view from the current view in android

I just started to study the android so im asking such a simple question. I tried to navigate(move from one view to another). This code shows no error and the button in first view is showing. But when i click the button nothing happens and app crashed.Can anyone please help me with where i'm going wrong in my code.

pushActivity.java

  package com.myapp.pus;

    import android.app.Activity;
    import android.content.Intent;

    import android.os.Bundle;
    import android.widget.Button;

    import android.view.View;
    public class PushActivity extends Activity {

        Button mybtn;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mybtn = (Button)findViewById(R.id.mybtn);
            mybtn.setOnClickListener(new View.OnClickListener(){

                public void onClick(View view) {
                    Intent nextScreen = new Intent(getApplicationContext(), SecondScreen.class);
                   // startActivity(new Intent(action));
                    startActivity(nextScreen);
                }
            });



    }
 }

main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/mybtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />

</LinearLayout>

Second Screen

package com.myapp.pus;

import android.app.Activity;
import android.os.Bundle;



public class SecondScreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);            
        // Binding Click event to Button    
    }    
}

Screen2.xml

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


</LinearLayout>

Thanks in advance.Hope for your help.

Upvotes: 0

Views: 199

Answers (6)

Devendra Anurag
Devendra Anurag

Reputation: 1

You need to implements the View.OnClickListener for the PushActivity Class. e.g.

public class Now extends Activity implements View.OnClickListener

Upvotes: 0

Dan
Dan

Reputation: 724

Replace getApplicationContext() with PushActivity.this

Also, you should be checking your logcat log for the exact error and stack trace to see exactly what line of code threw the exception or what the problem is.

Upvotes: 1

AndroidDev
AndroidDev

Reputation: 4559

In the AndroidManifest.xml file, Inside the application tag you should define your second activity which you want to show on click of button of first activity.

Eg:

    <application
    <activity
        android:name=".SecondActivity">
    </activity>
    </application>

Upvotes: 0

SathishBabu S
SathishBabu S

Reputation: 372

Declare secondscreen Activity in AndroidManifest.xml

Upvotes: 2

Aromal Sasidharan
Aromal Sasidharan

Reputation: 678

your declaration in manifest file is not correct.....

<activity android:name=".SecondScreen">
<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 

</activity>

change it to

<activity android:name=".SecondScreen"> <activity>

see the problem is that you have declared activity as main activity......

!11 !!! remove the 2 line

Upvotes: 1

Lucifer
Lucifer

Reputation: 29632

You need to define the SecondScreen Activity to your AndroidManifest.xml as below,

<activity android:name=".SecondScreen"></activity> before the tag is finish.

Upvotes: 2

Related Questions