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:
我见过很多人告诉你可以在oncreate方法之外设置setContentView,但我没有找到任何一个例子。现在,当我尝试使用setContentView时,我的应用程序崩溃了。这是我的源代码:
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?
更详细的说,我可以在oncreate方法中使用setContentView(R.layout.timepicker)而不会出现任何问题,因此问题是setContentView在New_ala.java类中无法正常工作。谁能帮我?
The code after setting the intent: AlarmActivity:
设置意图后的代码: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);
}
}
3 个解决方案
#1
3
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.
最好的方法是进行多项活动:而不是nou1.nou_alarma();,创建一个intent并使用新布局开始一个新活动。
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate
method of NewAlarmActivity
, set the content view to R.layout.timepicker
在NewAlarmActivity的onCreate方法中,将内容视图设置为R.layout.timepicker
#2
9
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.
您可以在事件(UI)线程上运行时调用setContentView。请注意,执行此操作时,需要重置通过调用findViewById初始化的任何字段。
#3
-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.
每个活动只能调用一次setContentView()方法。如果要在某个时刻完全更改布局,您应该选择ViewFlipper或在活动中使用2个布局,并通过调用view.setVisibility(View.GONE)在给定时间仅显示其中一个布局;和view.setVisibility(View.VISIBLE);分别。
#1
3
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.
最好的方法是进行多项活动:而不是nou1.nou_alarma();,创建一个intent并使用新布局开始一个新活动。
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate
method of NewAlarmActivity
, set the content view to R.layout.timepicker
在NewAlarmActivity的onCreate方法中,将内容视图设置为R.layout.timepicker
#2
9
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.
您可以在事件(UI)线程上运行时调用setContentView。请注意,执行此操作时,需要重置通过调用findViewById初始化的任何字段。
#3
-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.
每个活动只能调用一次setContentView()方法。如果要在某个时刻完全更改布局,您应该选择ViewFlipper或在活动中使用2个布局,并通过调用view.setVisibility(View.GONE)在给定时间仅显示其中一个布局;和view.setVisibility(View.VISIBLE);分别。