Can I use setContentView outside the oncreate method?
Asked Answered
C

3

9

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);
}
    }
Cryptoclastic answered 8/1, 2012 at 21:5 Comment(1)
"My app just crashes" please post the LogCat with the exceptionTeenybopper
T
4

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

Teenybopper answered 8/1, 2012 at 21:10 Comment(3)
Thanks for your reply. I created the new activity but when I run the program in the emulator, the screen gets black and nothing happens. This code after edit is in the description of my questionCryptoclastic
If you want us to investigate a crash, you need to post the content of the LogCat, that shows the exceptionTeenybopper
I found the problem. The program was always black because there was no condition for starting the new intent and right after the new intent started it got back to the main method and so on. Now I solved the problem and it works brilliantly. Thanks a lot.Cryptoclastic
A
10

You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.

Archbishop answered 8/1, 2012 at 21:38 Comment(0)
T
4

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

Teenybopper answered 8/1, 2012 at 21:10 Comment(3)
Thanks for your reply. I created the new activity but when I run the program in the emulator, the screen gets black and nothing happens. This code after edit is in the description of my questionCryptoclastic
If you want us to investigate a crash, you need to post the content of the LogCat, that shows the exceptionTeenybopper
I found the problem. The program was always black because there was no condition for starting the new intent and right after the new intent started it got back to the main method and so on. Now I solved the problem and it works brilliantly. Thanks a lot.Cryptoclastic
R
-4

The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Rhetoric answered 8/1, 2012 at 21:12 Comment(1)
The first part is simply wrong. setContentView can be called an arbitrary number of times.Archbishop

© 2022 - 2024 — McMap. All rights reserved.