如何从另一个视图设置视图?

时间:2014-05-29 10:51:38

标签: android

我想创建一个按下按钮后出现动画的应用程序。我正在使用带有文本和按钮的布局,我使用OnClickListener来了解按钮何时被按下。然后我使用setContentView来显示动画。

问题是,当我启动应用程序时它只是崩溃了。我试图直接显示动画(不使用布局),而且效果很好。

以下是代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity implements OnClickListener {

public static int sleeptime=22;
private static Paint paint = new Paint();
static boolean fill= true;
public static float y = 700;
public static float StepY_Figure =3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    View button1=findViewById(R.id.Button1);
    button1.setOnClickListener(this);       
}

@Override
public void onClick(View v){
    if(v.getId()==R.id.Button1){
        setContentView(new ExampleView(this));
    }
}

这就是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SplashScreenActivity"
android:background="#ffffff" >

<TextView
    android:id="@+id/text1"
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:text="Press the button when you are ready"/>

<Button
    android:id="@+id/Button1"
    android:text="Start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/text1"
    android:layout_alignParentLeft="true">
    </Button>

这是我的logcat:

D/dalvikvm(31502): threadid=10: interp stack at 0x4c0e6000
D/jdwp(31502): sendBufferedRequest : len=0x45
D/ActivityThread(31502): BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{4194eda8 com.example.respiracion}}
D/AndroidRuntime(31502): Shutting down VM
W/dalvikvm(31502): threadid=1: thread exiting with uncaught exception (group=0x4102d258)
E/AndroidRuntime(31502): FATAL EXCEPTION: main
E/AndroidRuntime(31502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.respiracion/com.example.respiracion.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime(31502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
E/AndroidRuntime(31502):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
E/AndroidRuntime(31502):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
E/AndroidRuntime(31502):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
E/AndroidRuntime(31502):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(31502):    at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime(31502):    at android.app.ActivityThread.main(ActivityThread.java:4624)
E/AndroidRuntime(31502):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(31502):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(31502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/AndroidRuntime(31502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/AndroidRuntime(31502):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(31502): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime(31502):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:213)
E/AndroidRuntime(31502):    at android.app.Activity.requestWindowFeature(Activity.java:3097)
E/AndroidRuntime(31502):    at com.example.respiracion.MainActivity.onCreate(MainActivity.java:26)
E/AndroidRuntime(31502):    at android.app.Activity.performCreate(Activity.java:4479)
E/AndroidRuntime(31502):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
E/AndroidRuntime(31502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
E/AndroidRuntime(31502):    ... 11 more
D/dalvikvm(31502): GC_CONCURRENT freed 165K, 4% free 9471K/9783K, paused 2ms+2ms

1 个答案:

答案 0 :(得分:1)

requestWindowFeature必须在setContentView

之前调用
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);