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 7862913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:18:48+00:00 2026-06-02T23:18:48+00:00

When i run application,Login Activity should come only once for first time and next

  • 0

When i run application,Login Activity should come only once for first time and next time ,when i open two different Activities should come based on button click in Login Activity.
In login activity i kept two radio buttons if i enable first radio button next time when i opened it should show main2 layout and if i enabled second radio button it should show main3 layout.

     public class Demo1 extends Activity {
        Button b1,b2;
        int count=0;
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      SharedPreferences settings = this.getSharedPreferences("MyApp",0);
      boolean firstrun=settings.getBoolean("firstrun",true);
      if (firstrun) {
        SharedPreferences.Editor e = settings.edit();
        e.putBoolean("firstrun",false);
        e.commit();


      setContentView(R.layout.main1);
      }
      else{
         test();
      }
      b1=(Button)findViewById(R.id.button1);
      b2=(Button)findViewById(R.id.button2);
     b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            count=1;
            Intent intent = new Intent(Demo1.this, ButtonActivate1.class);
              startActivity(intent);
        }
    });
     b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            count=2;
            // TODO Auto-generated method stub
            Intent intent = new Intent(Demo1.this, ButtonActivate1.class);
              startActivity(intent);
        }
    });




 }
private void test() {
    // TODO Auto-generated method stub
    if(count==1)
    {
        setContentView(R.layout.main2);
    }
    if(count==2)
    {
        setContentView(R.layout.main3);
    }
}

}

In this example when i am running it is getting force closed and showing error in the line button.setonclicklistner.please suggest me how to solve this issue.

  • 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-06-02T23:18:51+00:00Added an answer on June 2, 2026 at 11:18 pm

    I have updated my answer to the following. This should work for you now.

    package com.demo1;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    
    public class Demo1Activity extends Activity {
        private int count;
        private SharedPreferences settings;
    
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            settings = this.getSharedPreferences("MyApp",0);
            count = settings.getInt("count", 0);
    
            selectContentView();
        }
    
        public void button1Method(View v) {
            setCount(1);
            Intent intent = new Intent(Demo1Activity.this, Demo2Activity.class);
            startActivity(intent);
        }
    
        public void button2Method(View v) {
            setCount(2);
            Intent intent = new Intent(Demo1Activity.this, Demo3Activity.class);
            startActivity(intent);
        }
    
    
        private void selectContentView() {
            // TODO Auto-generated method stub
            switch (count) {
            case 1:
                setContentView(R.layout.main2);
                break;
            case 2:
                setContentView(R.layout.main3);
                break;
            default:
                setContentView(R.layout.main1);
            }
        }
    
        private void setCount(int count) {
            SharedPreferences.Editor e = settings.edit();
            e.putInt("count",count);
            e.commit();
        }
    }
    

    The main1.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="main1" />
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:onClick="button1Method"
                android:text="button1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:onClick="button2Method"
                android:text="button2" />
        </LinearLayout>
    
    </RelativeLayout>
    

    I have replaced the onClick listeners to be set within the XML file. I find this neater and easier.

    Manifest file:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.demo1"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="10" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".Demo1Activity"
                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=".Demo2Activity"
                android:label="@string/app_name" />
            <activity
                android:name=".Demo3Activity"
                android:label="@string/app_name" />
        </application>
    
    </manifest>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two activities; Home is my first activity and Settings is my second
I'm trying to write an android application that has two main activities: login screen
I have an activity that i only want to run when the application is
I developed one swing application but each time you run application new window is
I have an ASP.net login control in my web application. While I run the
I create sample application in that I was used first two Qwidget from UI
In my application I want to show a login form first and then the
I have a Silverlight application that cannot login when it installed in a different
Right now whenever I run application It creates a file (file name specified in
Question: What command do you type in to the Run Application box on ubuntu

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.