我见过很多人告诉您可以在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>

更详细地讲,我可以在oncreate方法内使用setContentView(R.layout.timepicker),而不会出现任何问题,因此问题在于setContentView在New_ala.java类内部无法正常工作。谁能帮我?

设置 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);
}
    }

最佳答案

最好的方法是进行多个 Activity :代替nou1.nou_alarma();,创建一个 Intent 并使用新的布局开始一个新的 Activity 。

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

然后在onCreateNewAlarmActivity方法中,将内容 View 设置为R.layout.timepicker

关于java - 我可以在oncreate方法之外使用setContentView吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8781323/

10-11 02:10