我如何在RelativeLayout中设置setContentView? (进度条)

时间:2012-03-30 06:56:24

标签: android progress-bar nullpointerexception

有人可以向我解释我如何在相对布局中设置setContentView吗? 我添加了一个活动,但我收到错误,无效指针......

我试过这个:

         public ProgressbarActivity(Context context) {
    super(context);

    Activity a = new Activity();
    RelativeLayout rl = new RelativeLayout(getContext());
    LinearLayout k = new LinearLayout(getContext());
    k.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    LinearLayout z = new LinearLayout(getContext());
    z.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    RoundRectShape s = new RoundRectShape(f, null, null);
    ShapeDrawable shapedrawable =  new ShapeDrawable(s);
    shapedrawable.setShape(s);
    shapedrawable.getPaint().setColor(0xffffffff);


    RoundRectShape s1 = new RoundRectShape(f, null, null);
    ShapeDrawable sd =  new ShapeDrawable(s1);
    sd.setShape(s1);
    sd.getPaint().setColor(0xff0080ff);
    rl.setLayoutParams(new LayoutParams(200, 25));

    LayoutParams lp = new LayoutParams(200, 20);
    rl.setPadding(0, 100, 0, 0);
    rl.setGravity(Gravity.CENTER_HORIZONTAL);


    ImageView iv = new ImageView(getContext());
    iv.setBackgroundDrawable(shapedrawable);
    iv.setLayoutParams(lp);


    GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colours);


    ImageView iv3 = new ImageView(getContext());
    iv3.setBackgroundDrawable(g);
    iv3.setLayoutParams(lp);

    ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    ImageView iv2 = new ImageView(getContext());
    iv2.setBackgroundDrawable(cd);
    iv2.setLayoutParams(lp);
    cd.setLevel(5000);


    rl.addView(iv);
    rl.addView(iv2);
    rl.addView(iv3);
    a.setContentView(rl);

    onCreate();

}

我做错了什么? 有人可以解释一下吗?

我收到了nullPointerException ...

这是我的logCat:

03-30 06:58:27.369: E/AndroidRuntime(1534): FATAL EXCEPTION: main
03-30 06:58:27.369: E/AndroidRuntime(1534): java.lang.RuntimeException: Unable to start activity ComponentInfo{eng.pim.app/eng.pim.app.PimActivity}: java.lang.NullPointerException
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.os.Looper.loop(Looper.java:137)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at java.lang.reflect.Method.invoke(Method.java:511)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at dalvik.system.NativeStart.main(Native Method)
03-30 06:58:27.369: E/AndroidRuntime(1534): Caused by: java.lang.NullPointerException
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.Activity.setContentView(Activity.java:1855)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at eng.pim.app.ProgressbarActivity.<init>(ProgressbarActivity.java:77)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at eng.pim.app.PimActivity.onCreate(PimActivity.java:24)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.Activity.performCreate(Activity.java:4465)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-30 06:58:27.369: E/AndroidRuntime(1534):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-30 06:58:27.369: E/AndroidRuntime(1534):     ... 11 more

1 个答案:

答案 0 :(得分:1)

您的代码有很多问题:

1)如果ProgressbarActivityActivity使用构造函数进行初始化,则必须使用{{1}初始化视图,构建数据,设置线程等的方法。

2)你永远不会这样做:

onCreate()

如果您要使用您构建的布局开始新活动,则必须使用Activity a = new Activity(); 开始新活动:

Intent

以上代码适用于某项活动,否则您必须获取对某些Intent i = new Intent(CurrentActivityName.this, OtherActivityName.class); startActivity(i); 的引用,而不是CurrentActivityName.this(您可以使用方法Context使用应用程序上下文并且您还必须在getApplicationContext()引用上致电startActivity()。如果您希望新活动具有您构建的布局,请在新活动Context方法中移动该代码,请在onCreate()处使用您构建的setContentView()并使用上述代码使用Relativelayout启动该活动。 (注意:您必须在清单中声明新活动)

3)Intent堆叠视图(一个在其他顶部),如果这不是您想要的,那么使用规则正确地将视图定位在该布局中

也许你应该看一些关于如何做的教程:

- 用于RelativeLayout创建并启动一个:http://developer.android.com/guide/topics/fundamentals/activities.html

-for Activity使用:http://developer.android.com/resources/tutorials/views/hello-relativelayout.html