0

So I am programmatically adding buttons to an already existing RelativeLayout. I get a fatal error where running this code without any error message. My android app doesn't even open. Though when I take out the if/else below it works fine.

Am I not allowed to use an if statement to populate a layout or am I implementing this the wrong way?

*Note that below code is only pseudo code. Everything works as expected when the if/else is commented out **edit adding full code and logcat

protected void onCreate(Bundle savedInstanceState)
{
    //load main screen
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);

    boolean bool = getBool();
    if(bool)
    {
        //some parameters for the sign in button
        RelativeLayout.LayoutParams button1Param = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //make the new button and set attributes
        Button button1 = new Button(this); 
        button1.setText("b1"); 
        button1.setLayoutParams(button1Param);
        button1.setId(1);
        //create an on click listener to start the activity
        button1.setOnClickListener(new OnClickListener() {
            //on click execute activity
            @Override
            public void onClick(View v) {
                startActivity(new Intent(v.getContext(), FirstActivity.class));
            }
        });
    }
    else
    {
        //some parameters for the status button
        RelativeLayout.LayoutParams statusParam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        ToggleButton buttonStatus = new ToggleButton(this); 
        buttonStatus.setText("Status"); 
        buttonStatus.setLayoutParams(statusParam);
        buttonStatus.setId(2);
        relativeLayoutTop.addView(buttonStatus);
    }
 }

Log is here:

07-16 10:33:52.900: E/AndroidRuntime(1648): FATAL EXCEPTION: main
07-16 10:33:52.900: E/AndroidRuntime(1648): Process: shout.locate, PID: 1648
07-16 10:33:52.900: E/AndroidRuntime(1648): java.lang.RuntimeException: Unable to start activity ComponentInfo{shout.locate/shout.locate.MainActivity}: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.os.Looper.loop(Looper.java:136)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at java.lang.reflect.Method.invoke(Method.java:515)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at dalvik.system.NativeStart.main(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648): Caused by: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648):     at shout.locate.MainActivity.initLayout(MainActivity.java:79)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at shout.locate.MainActivity.onCreate(MainActivity.java:57)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.Activity.performCreate(Activity.java:5231)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 10:33:52.900: E/AndroidRuntime(1648):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 10:33:52.900: E/AndroidRuntime(1648):     ... 11 more
Onik
  • 19,396
  • 14
  • 68
  • 91
simonshout
  • 95
  • 3
  • 14
  • 1
    Please add the real code, and check logcat for the stacktrace – Marco Acierno Jul 16 '14 at 14:28
  • You need to show the actual code so we know which part you are doing wrong. You can add Views programmatically so you are doing something wrong inside there. And you are getting an error message in your logcat...you need to post that. – codeMagic Jul 16 '14 at 14:28
  • Attach your logcat. We can't do anything with _"I have an error"_ :) – AlonsoFloo Jul 16 '14 at 14:30
  • Slow start to my day haha, just uploaded all the details. I hope it's enough! – simonshout Jul 16 '14 at 14:43
  • I just realized I spent the last 12 hours trying to figure out the issue but your comment just helped me realize I'm returning a null value.. Boy do I feel foolish. Thanks a bunch though! – simonshout Jul 16 '14 at 15:03

1 Answers1

1

Try this:

protected void onCreate(Bundle savedInstanceState)
 {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);
  if(someboolean)
  {
       //set layouparams
       //new button1
       //button1.setonclicklistener etc etc
       //set new layoutparams
       //new button2
       //button2.setonclicklistener etc etc
  }
  else
  {
       //set layouparams
       //new button3
       //button.setonclicklistener etc etc
  }
}

You should get your layout only after setContentView(R.layout.activity_main);

EDIT

  1. You are not adding the button to layout in if statement

  2. v.getContext() might be null.Use YourCurrentActivity.this instead.

JiTHiN
  • 6,548
  • 5
  • 43
  • 69