1

私は xml をインフレートしており、この xml からビューを作成しています。画像の幅を変更する必要がありますが、NullPointerException が発生します。

私が持っているコードは次のとおりです。

for (int o = 0; o < study.size(); o++) {
     LayoutInflater li_est= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View vEst = li_est.inflate(R.layout.study_details, null);

 line = (ImageView)findViewById(R.id.img_line);

 if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
      **// HERE NEED CHANGE WIDTH OF LINE BUT I GET THE EXCEPTION**
}

私は次の方法でそうしようとしました:

 1.     line.getLayoutParams().width = 20;
 2.     line.setMaxWidth(20);
 3.     line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

問題はインフレによるものだと思いますが、解決策はあります。ありがとうございます。英語が下手で申し訳ありません。

4

2 に答える 2

2
 final View vEst = li_est.inflate(R.layout.study_details, null);
 line = (ImageView)vEst.findViewById(R.id.img_line);

おそらくラインはstud_レイアウト内で宣言されているので、それを取得するにはオブジェクトdetailsが必要ですvEst

于 2012-11-30T08:30:29.427 に答える
1

膨張したビューで findViewById() を実行する必要があります...これを行います:

最終ビュー vEst = li_est.inflate(R.layout.study_details, null); line = (ImageView) vEst .findViewById(R.id.img_line);

于 2012-11-30T08:31:29.363 に答える