启动时的致命异常

时间:2012-02-22 22:10:58

标签: android layout widget

我是一个初学者,正在编写一个简单的程序化UI,尽管我已经多次重新开始,但我不知道是什么打破了这个代码,甚至不能首先显示UI。我必须做一些错误的观点,但我不知道是什么。有人可以帮忙吗?

这是我的主要活动代码(现在都是)

package com.orbitbreak.musicbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.orbitbreak.musicbox.R;

public class MusicBoxActivity extends Activity {

    public ViewGroup viewUpdate;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // makes one image layout
        ImageView singleButton = new ImageView(this);
        singleButton.setImageResource(R.drawable.gridbuttonoff);
        singleButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        viewUpdate.addView(singleButton);

        redrawView(getViewUpdate());

    }

    public void redrawView(ViewGroup param){
        setViewUpdate(param);
        setContentView(getViewUpdate());
    }
    public ViewGroup getViewUpdate(){
        return this.viewUpdate;
    }
    public void setViewUpdate(ViewGroup param){
        this.viewUpdate = param;
    }

}

这是我的logcat日志

02-22 16:53:56.440: D/AndroidRuntime(625): Shutting down VM
02-22 16:53:56.440: W/dalvikvm(625): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
02-22 16:53:56.460: E/AndroidRuntime(625): FATAL EXCEPTION: main
02-22 16:53:56.460: E/AndroidRuntime(625): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.orbitbreak.musicbox/com.orbitbreak.musicbox.MusicBoxActivity}:     java.lang.NullPointerException
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.os.Handler.dispatchMessage(Handler.java:99)
02-22 16:53:56.460: E/AndroidRuntime(625):  at android.os.Looper.loop(Looper.java:123)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread.main(ActivityThread.java:3683)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     java.lang.reflect.Method.invokeNative(Native Method)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     java.lang.reflect.Method.invoke(Method.java:507)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-22 16:53:56.460: E/AndroidRuntime(625):  at dalvik.system.NativeStart.main(Native Method)
02-22 16:53:56.460: E/AndroidRuntime(625): Caused by: java.lang.NullPointerException
02-22 16:53:56.460: E/AndroidRuntime(625):  at     com.orbitbreak.musicbox.MusicBoxActivity.onCreate(MusicBoxActivity.java:25)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 16:53:56.460: E/AndroidRuntime(625):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-22 16:53:56.460: E/AndroidRuntime(625):  ... 11 more

1 个答案:

答案 0 :(得分:2)

viewUpdate 为空,您是如何计划对该视图进行充气的?它应该是你的根视图吗?

您应该考虑从xml填充UI。这是非常方便和推荐的Android方式。

修改

我相信这正是你想要的:GridView example