0

所以我以编程方式将按钮添加到已经存在的RelativeLayout. 在没有任何错误消息的情况下运行此代码时出现致命错误。我的安卓应用程序甚至没有打开。虽然当我拿出if/else下面的时候它工作正常。

我是否不允许使用 anif statement来填充布局,或者我是否以错误的方式实现了这一点?

*请注意,以下代码仅为伪代码。if/else当注释掉 **edit 添加完整代码和 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);
    }
 }

日志在这里:

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
4

1 回答 1

1

尝试这个:

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
  }
}

你应该只在之后得到你的布局setContentView(R.layout.activity_main);

编辑

  1. 您没有在if语句中将按钮添加到布局

  2. v.getContext()可能是null.UseYourCurrentActivity.this 代替。

于 2014-07-16T14:30:53.967 回答