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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:19:26+00:00 2026-05-28T02:19:26+00:00

I have seen many people telling that you can set setContentView outside the oncreate

  • 0

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn’t find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:

AlarmActivity.java:

package com.alarm.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

public class AlarmActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button buton = new Button(this);
        buton.setId(101);
        buton.setText("New alarm");
        buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        RelativeLayout layout1 = new RelativeLayout(this);  
        layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
    layout1.addView(buton);  
    setContentView(layout1); 

    Button nou =(Button)findViewById(101);
    nou.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            New_ala nou1=new New_ala();
            nou1.nou_alarma();
            }
        });
    }
}

New_ala.java:

package com.alarm.example;

public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);        
}
}

TimePicker.xml:

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


    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true">

        <Button 
        android:id="@+id/picker_save" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Save">
        </Button>

        <Button 
        android:id="@+id/picker_cancel" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Cancel">
        </Button>
</LinearLayout>

</LinearLayout>

On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn’t working properly inside the New_ala.java class. Can anyone help me?

The code after setting the intent:
AlarmActivity:

package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(this, NewAlarmActivity.class));
        }
    }

NewAlarmActivity:

package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timepicker);
}
    }
  • 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-28T02:19:27+00:00Added an answer on May 28, 2026 at 2:19 am

    The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.

    startActivity(new Intent(this, NewAlarmActivity.class));
    

    And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker

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

Sidebar

Related Questions

I have seen many answers on stackoverflow, but I didn't find an answer that
I have seen in this forum, that many of the people are doing string
I have seen many people writing that Java ME (J2ME) is dying. Is it
I have seen the trend that many people don't like to use exceptions in
I have seen in many times in JavaScript code people add a return true
I have seen many apps that take instrument classes and take -javaagent as a
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I am a newbie in cryptographic system but i have seen many sources tell
i have seen many answers when people ask how to grab and extract the
I have seen many posts about people migrating Master Pages from 2007 to 2010

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.