我有一张图片,我想创建片段并显示此图片,但线条中有错误。
ImageView.setImageResource(AndroidImageAssete.getmeads.get(0));
这是我的java文件的代码:
public class BodyPartFragment extends Fragment {
public BodyPartFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_body_part, container, false);
ImageView imageView=(ImageView) rootView.findViewById(R.id.fragment_body_part);
// Inflate the layout for this fragment
ImageView.setImageResource(AndroidImageAssete.getmeads.get(0));
return rootView;
}
}
主要活动:
BodyPartFragment bodyPartFragment=new BodyPartFragment();
getFragmentManager().beginTransaction()
.add(R.id.contener,contener);
.commit();
}
}
这是错误:
Information:Gradle tasks [:app:assembleDebug]
/Users/mac/Downloads/Flagment/app/src/main/java/com/example/waadalkatheri/flagment/MainActivity.java
Error:(16, 9) error: illegal start of expression
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 50.036 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console
答案 0 :(得分:1)
只需替换此行
ImageView.setImageResource(AndroidImageAssete.getmeads.get(0));
这一行
imageView.setImageResource(R.drawable.abc);
答案 1 :(得分:0)
String imageFromDrawable = "@drawable/myresource";
int imageResource = getResources().getIdentifier(imageFromDrawable, null, getPackageName());
ImageView imageview = (ImageView) rootView.findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);