Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3982558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:32:31+00:00 2026-05-20T05:32:31+00:00

I am learning the concept of Intent s and the application I am working

  • 0

I am learning the concept of Intents and the application I am working on crashes when i try to go from one intent to another,the intent object is fired on onClickListener() of a button.

Below is the code:

intentdemo.java: first activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class intentdemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button btn1= (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(intentdemo.this,Intentdemo2.class);
                startActivity(i);
            }

       });




    }
}

Intentdemo2.java: Activity 2

package com.test.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Intentdemo2 extends Activity {
    @Override

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.button2);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getBaseContext(),intentdemo.class);
                startActivity(i);
            }
        });

    }

}

AndroidManifest.xml

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


<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".intentdemo"
              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=".Intentdemo2"></activity>
</application>
</manifest>

main.xml: for first activity

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button 
    android:text="Button" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

layout.xml: for second activity

<?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">
    <Button android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
    <TextView 
    android:text="@string/text" 
    android:id="@+id/textView1" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></TextView>
</LinearLayout>

I would appreciate if someone can point me towards my mistake here, first Activity is displayed, When i click on button it gives me the message “the activity has stopped unexpectedly”, how should I debug in case the error in activity is not intuitive.

Thanks,
Sid

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:32:31+00:00Added an answer on May 20, 2026 at 5:32 am

    Your second activity is loading its layout from main.xml (setContentView(R.layout.main);), but is trying find button with id R.id.button2. There’s no such button in the main layout, so attempting to set the click listener is crashing on trying to access null object.

    The fix is simple – change the line to setContentView(R.layout.layout);. (On a separate note – change the name of that file from layout.xml to something else)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm self-learning the REST concept and am playing with the Flickr API. One of
I'm learning iOS and I'm working on what amounts to a Proof of Concept
I have just started learning the concept of Object oriented programming and have put
I am learning about pointers and one concept is troubling me. I understand that
I start learning some java concurrency concept ans put in use. But one of
I'm learning C++ from book. I think I understand basic concept but the book
I am learning Servlets concept. Initially, I Tutorial referred to this link and working
I'm learning about Caja, and I am confused about the concept of this-stealing: Another
I am learning concepts related to .NET framework. I am confused at one point.
In a process of leaning java serialization concept, i was puzzled at one point.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.