Reputation:
I am a beginner writing a simple programmatic UI, and even though I've started over again many times carefully, I can't tell what's breaking this code to the point where it won't even display the UI first. I've got to be doing something wrong with Views, but I can't tell what. Can anybody help?
This is my main activity code (all there is, right now)
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;
}
}
and here's my logcat log
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
Upvotes: 0
Views: 134
Reputation: 11592
viewUpdate is null, how did you plan to inflate that view? Is it supposed to be your root view?
You should really consider populating your UI from xml. It's very convenient and the recommended Android way.
EDIT
I believe this is exactly what you want: GridView example
Upvotes: 2