如何创建一个新对象并以编程方式将layout_centerVertical设置为true?

时间:2015-10-24 16:20:49

标签: android

我以编程方式创建了一个ImageView,我想设置layout_centerVertical =" true"和android:layout_centerHorizo​​ntal =" true"。

我试过这段代码:

    ImageView myImage = new ImageView(this);
    Resources res = getApplicationContext().getResources();
    myImage.setImageResource(res.getIdentifier((card.getColor() + "_" + card.getFigure()).toLowerCase(), "drawable", this.getPackageName()));
    myImage.setTag(IMAGEVIEW_TAG);

    myImage.setOnTouchListener(new MyTouchListener());

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.toplinear);
    layout.addView(myImage);

    View positiveButton = myImage.getRootView();
    RelativeLayout.LayoutParams layoutParams =
            (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    positiveButton.setLayoutParams(layoutParams);

但是logcat会抛出错误:

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法' void android.widget.RelativeLayout $ LayoutParams.addRule(int,   INT)'在null对象引用上               在pl.ds13ds14.ucp.CardsActivity.AddCard(CardsActivity.java:55)

第55行包含:

layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

1 个答案:

答案 0 :(得分:0)

试试这个:

$src = $request->path;
$dest = "/home/$randomNum/serverFiles";

echo $ssh->exec("adduser --force-badname $randomNum");
echo $ssh->exec("echo \"$randomNum:test123\" | chpasswd");

echo $ssh->exec("cp -r $src $dest");
echo $ssh->exec("usermod -d $dest $randomNum");

echo $ssh->exec("cd $dest && ./samp03svr");

$ssh->disconnect();